【问题标题】:Java Transformer error: Could not compile stylesheetJava Transformer 错误:无法编译样式表
【发布时间】: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 中工作时使用过它。

标签: java xml xslt


【解决方案1】:

要使用 XSLTC,请将 xalan.jar(2.5)、serializer.jar、xml-apis.jar 和 xercesImpl.jar 放在您的类路径中。

【讨论】:

  • 不要这样做。自 2010 年以来,所有这些包(来自 Xerces 项目)都已被废弃。代码现在位于 JVM 中。
【解决方案2】:

您必须从您拥有的 xslt 字符串构造一个流,然后将其用作流源

InputStream xslStream = new ByteArrayInputStream(XSLTRule.getBytes("UTF-8"));
Source xslInput = new StreamSource(xslStream);

将结果转换为字符串:

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Result result = new StreamResult(bos);
    transformer.transform(xmlInput, result);
    String s = new String(bos.toByteArray());
    System.out.println(s);

【讨论】:

  • 非常感谢!但我仍然对Result result = new StreamResult(); 有疑问。在那里做什么?
【解决方案3】:

constructor

public StreamSource(String systemId)

从 URL 构造一个 StreamSource。我认为您正在传递 XSLT 的内容。试试这个:

File xslFile = new File("path/to/your/xslt");
TransformerFactory factory = TransformerFactory.newInstance();
Templates xsl = factory.newTemplates(new StreamSource(xslFile));

您还必须设置您的StreamResult 将写入的OutputStream

ByteArrayOutputStream baos = new ByteArrayOutputStream();
Result result = new StreamResult(baos);
transformer.transform(xmlInput, result);
return baos.toString();

【讨论】:

  • 或者,只需将new StreamSource(XSLTRule) 更改为new StreamSource(new StringReader(XSLTRule))
  • 非常感谢!但我仍然对Result result = new StreamResult(); 有疑问。在那里做什么?
  • @Ivan:Result result = new StreamResult(); 有什么问题?
  • @Michael 对于 transformer.transform 行,我得到了这个异常:javax.xml.transform.TransformerException: Result object passed to ''{0}'' is invalid.我对变量 XML 做了与 XSLTRule 相同的事情。
  • @Ivan:我明白了:我已经编辑了我的答案来解决这个问题。
猜你喜欢
  • 2016-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-06
  • 1970-01-01
  • 1970-01-01
  • 2012-10-21
  • 1970-01-01
相关资源
最近更新 更多