提问者:小点点

如何用JAVA在eclipse indigo中使用Saxon(XSLT2.0处理器)


我必须使用 XSLT 2.0 处理器来实现字符串操作函数,如 replace()。我在 POM 文件中添加了撒克逊的依赖项并运行了“mvn install”命令。通过执行此操作,“saxon-9.1.0.8.jar”被添加到“引用的库”文件夹下。

在代码中,我使用了System.set属性("javax.xml.transform.TransformerWorks","net.sf.saxon.TransformerFactoryImpl");

当我尝试调用下面一行< code > transformer factory . new instance(" net . SF . Saxon . transformer factory impl ",null);
我收到一条错误消息

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

如果我尝试调用新的net.sf.saxon。转换器FactoryImpl(),我得到的错误为java.lang.NoClassDefFoundError:net/sf/saxon/TransformerFactoryImpl

请让我知道,如果我在配置撒克逊与日食靛蓝的东西。


共3个答案

匿名用户

请确保您已在构建路径中包含撒克逊罐。然后在源代码中,以下行应该有效:

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

匿名用户

我同意迈克尔的观点,你没有在你的类路径上正确地包含Saxon jar文件。我想在eclipse中有一个选项可以通过单击项目-构建路径-添加jar来添加jar文件(类似的东西。)。试试看,构建你的工作区可能会有所帮助。

匿名用户

下面的简单示例:

确保 saxon9ee.jar 在您的类路径中

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>