2013-05-02 111 views
1

我必須使用XSLT 2.0處理器來處理字符串操作函數,如replace()。我在POM文件中添加了saxon的依賴項,並運行「mvn install」命令。通過執行「saxon-9.1.0.8.jar」添加到「Referenced Libraries」文件夾下。如何在eclipse indigo中使用Saxon(XSLT 2.0處理器)與JAVA

在我使用System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");

當我嘗試調用下面一行TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null);
我收到一個錯誤說

javax.xml.transform.TransformerFactoryConfigurationError: Provider net.sf.saxon.TransformerFactoryImpl not found. 

如果我嘗試調用new net.sf.saxon.TransformerFactoryImpl();的代碼,我得到的錯誤作爲java.lang.NoClassDefFoundError: net/sf/saxon/TransformerFactoryImpl

請讓我知道如果我錯過了配置日食靛藍撒克遜的東西。

+0

當你在jar文件中查看時,它是否顯示類? – flup 2013-05-02 11:33:16

+0

如果你使用Maven,你應該使用Saxon HE。從這裏獲取最新信息http://mvnrepository.com/artifact/net.sf.saxon/Saxon-HE – artbristol 2013-05-02 11:52:49

+0

這些消息都表明Saxon jar文件不在類路徑中。但我不太瞭解配置Eclipse-indigo以瞭解爲什麼會出現這種情況。 – 2013-05-05 20:05:15

回答

0

我同意邁克爾你沒有正確地在你的課程路徑中包含撒克遜jar文件。我想在eclipse中有一個選項可以通過點擊項目 - 構建路徑 - 添加jar(類似於這個)來添加jar文件。試一試,並建立你的工作空間,可能會有所幫助。

1

請確保您已將saxon jar包含在構建路徑中。然後,在你的源代碼以下行應該工作:

System.setProperty("javax.xml.transform.TransformerFactory", 
        "net.sf.saxon.TransformerFactoryImpl"); 
TransformerFactory tfactory = TransformerFactory.newInstance(); 
0

簡單的例子如下:

確保saxon9ee.jar是在classpath

movie.xml

<?xml version="1.0" encoding="UTF-8"?> 
<movies> 
    <movie> 
     <title>Empire Burlesque</title> 
     <artist>Bob Dylan</artist> 
     <country>USA</country> 
     <company>Columbia</company> 
     <price>10.90</price> 
     <year>1985</year> 
    </movie> 
    <movie> 
     <title>Hide your heart</title> 
     <artist>Bonnie Tyler</artist> 
     <country>UK</country> 
     <company>CBS Records</company> 
     <price>9.90</price> 
     <year>1988</year> 
    </movie> 
    <movie> 
     <title>Greatest Hits</title> 
     <artist>Dolly Parton</artist> 
     <country>USA</country> 
     <company>RCA</company> 
     <price>9.90</price> 
     <year>1982</year> 
    </movie> 
    <movie> 
     <title>Still got the blues</title> 
     <artist>Gary Moore</artist> 
     <country>UK</country> 
     <company>Virgin records</company> 
     <price>10.20</price> 
     <year>1990</year> 
    </movie> 
    <movie> 
     <title>Eros</title> 
     <artist>Eros Ramazzotti</artist> 
     <country>EU</country> 
     <company>BMG</company> 
     <price>9.90</price> 
     <year>1997</year> 
    </movie> 
    <movie> 
     <title>One night only</title> 
     <artist>Bee Gees</artist> 
     <country>UK</country> 
     <company>Polydor</company> 
     <price>10.90</price> 
     <year>1998</year> 
    </movie> 
</movies> 

movie.xsl

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    version="1.0"> 
    <xsl:template match="/"> 
     <html> 
      <body> 
       <h2>Movies</h2> 
       <table border="1"> 
        <tr bgcolor="#9acd32"> 
         <th style="text-align:left">Title</th> 
         <th style="text-align:left">Artist</th> 
         <th style="text-align:left">Country</th> 
        </tr> 
        <xsl:for-each select="movies/movie"> 
         <xsl:sort select="country"></xsl:sort> 

         <!-- IF CONDITION --> 
         <xsl:if test="country='USA'"> 
          <tr> 
           <td> 
            <xsl:value-of select="title" /> 
           </td> 
           <td> 
            <xsl:value-of select="artist" /> 
           </td> 
           <td> 
            <xsl:value-of select="country" /> 
           </td> 
          </tr> 
         </xsl:if> 
        </xsl:for-each> 
       </table> 
      </body> 
     </html> 
    </xsl:template> 
</xsl:stylesheet> 

SaxonDemo.java

import java.io.File; 

import javax.xml.transform.Transformer; 
import javax.xml.transform.TransformerConfigurationException; 
import javax.xml.transform.TransformerException; 
import javax.xml.transform.TransformerFactory; 
import javax.xml.transform.stream.StreamResult; 
import javax.xml.transform.stream.StreamSource; 

public class SaxonDemo { 

    public static void main(String[] args) throws Exception { 
    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); 
    transform("movie.xml", "movie.xsl"); 
    } 

    public static void transform(String xmlFile, String xslFile) throws TransformerException, 
     TransformerConfigurationException { 

    TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
    Transformer transformer = transformerFactory.newTransformer(new StreamSource(new File(xslFile))); 
    transformer.transform(new StreamSource(new File(xmlFile)), new StreamResult(System.out)); 
    } 
} 

在命令行運行

C:\Users\ranjiths>java -jar C:\ranjiths\T3\ws\XSLTDemo\lib\saxon9ee.jar C:\ranjiths\T3\ws\XSLTDemo\testdata\xml\movie.xml C:\ranjiths\T3\ws\XSLTDemo\testdata\xsl\movie.xsl 

輸出:

<html> 
    <body> 
     <h2>Movies</h2> 
     <table border="1"> 
     <tr bgcolor="#9acd32"> 
      <th style="text-align:left">Title</th> 
      <th style="text-align:left">Artist</th> 
      <th style="text-align:left">Country</th> 
     </tr> 
     <tr> 
      <td>Empire Burlesque</td> 
      <td>Bob Dylan</td> 
      <td>USA</td> 
     </tr> 
     <tr> 
      <td>Greatest Hits</td> 
      <td>Dolly Parton</td> 
      <td>USA</td> 
     </tr> 
     </table> 
    </body> 
</html>