我必须使用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配置撒克逊的话,请告诉我。
答案 0 :(得分:1)
请确保您已在构建路径中包含saxon
jar。然后在您的源代码中,以下行应该有效:
System.setProperty("javax.xml.transform.TransformerFactory",
"net.sf.saxon.TransformerFactoryImpl");
TransformerFactory tfactory = TransformerFactory.newInstance();
答案 1 :(得分:0)
我同意迈克尔你没有正确地在你的类路径中包含Saxon jar文件。我想在eclipse中有一个选项可以通过单击项目来添加jar文件 - 构建路径 - 添加jar(类似于此的东西。)。试一试,建立可能有用的工作空间。
答案 2 :(得分:0)
以下简单示例:
确保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>