1

我必须使用 XSLT 2.0 处理器来处理字符串操作功能,例如replace(). 我在 POM 文件中添加了 saxon 的依赖项并运行了“mvn install”命令。通过这样做,“saxon-9.1.0.8.jar”被添加到“Referenced Libraries”文件夹下。

在我使用的代码中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

如果我在使用 eclipse indigo 配置撒克逊语时遗漏了什么,请告诉我。

4

3 回答 3

1

请确保您已saxon在构建路径中包含 jar。然后在您的源代码中,以下几行应该可以工作:

System.setProperty("javax.xml.transform.TransformerFactory",
                   "net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tfactory = TransformerFactory.newInstance();
于 2013-05-19T11:36:19.707 回答
0

我同意 Michael 的观点,您没有正确地将 Saxon jar 文件包含在您的类路径中。我猜eclipse中有一个选项可以通过单击项目-构建路径-添加jar(类似于this。)来添加jar文件。试一试,构建可能有帮助的工作区。

于 2013-05-17T06:09:11.233 回答
0

下面的简单示例:

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

电影.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>

电影.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>
于 2016-07-20T10:55:30.630 回答