【发布时间】:2011-07-25 05:52:08
【问题描述】:
我想用 Java 中的 XSLT 转换 XML。为此,我使用了javax.xml.transform 包。但是,我得到了异常javax.xml.transform.TransformerConfigurationException: Could not compile stylesheet。这是我正在使用的代码:
public static String transform(String XML, String XSLTRule) throws TransformerException {
Source xmlInput = new StreamSource(XML);
Source xslInput = new StreamSource(XSLTRule);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer(xslInput); // this is the line that throws the exception
Result result = new StreamResult();
transformer.transform(xmlInput, result);
return result.toString();
}
请注意,我标记了引发异常的行。
当我输入方法时,XSLTRule的值是这样的:
<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
xmlns:msxsl='urn:schemas-microsoft-com:xslt'
exclude-result-prefixes='msxsl'
xmlns:ns='http://www.ibm.com/wsla'>
<xsl:strip-space elements='*'/>
<xsl:output method='xml' indent='yes'/>
<xsl:template match='@* | node()'>
<xsl:copy>
<xsl:apply-templates select='@* | node()'/>
</xsl:copy>
</xsl:template>
<xsl:template match="/ns:SLA
/ns:ServiceDefinition
/ns:WSDLSOAPOperation
/ns:SLAParameter[@name='Performance']"/>
</xsl:stylesheet>
【问题讨论】:
-
你捕捉到了哪些异常?
-
它是
javax.xml.transform.TransformerConfigurationException。我编辑了我的原始帖子,它现在在那里。 -
为什么你的最后一个模板是空的?
-
因为该元素应该被删除。它找到元素,但不复制它。这个 XSLT 应该可以工作,我以前在 .NET 中工作时使用过它。