JAXP管道忽略xsl:comment' s(使用Saxon HE 9.5.1-4)

时间:2014-03-18 13:24:35

标签: xslt comments saxon jaxp

使用JAXP pipelin(使用Saxon HE)时,创建的注释不会出现在结果.xml中。

首先,我设置系统属性以获取信息并使用Saxon并定义输入/输出:

System.setProperty("jaxp.debug", "1");
System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
StreamSource xsl = ...
StreamResult output = ...
InputSource input = ...

然后,我有以下构造,使用虚拟Pre和Post滤镜:

TransformerFactory factory = TransformerFactory.newInstance();
SAXTransformerFactory saxFactory = (SAXTransformerFactory) factory;
SAXParserFactory parserFactory = SAXParserFactory.newInstance();

parserFactory.setNamespaceAware(true);
XMLReader parser = parserFactory.newSAXParser().getXMLReader();
XMLFilter pre = new XMLFilterImpl(parser);
XMLFilter xslFilter = saxFactory.newXMLFilter(xsl);
xslFilter.setParent(pre);
XMLFilter post = new XMLFilterImpl(xslFilter);

TransformerHandler serializer = saxFactory.newTransformerHandler();
serializer.setResult(output);
Transformer trans = serializer.getTransformer();
trans.setOutputProperty(OutputKeys.METHOD, "xml");

post.setContentHandler(serializer);
post.parse(input);

使用以下样式表运行时:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
        <xsl:comment>Nice comment</xsl:comment>
        <test>[<xsl:value-of select="system-property('xsl:vendor')" />] 
            (<xsl:value-of select="system-property('xsl:version')" />
            )[<xsl:value-of select="system-property('xsl:vendor-url')" />]</test>
    </xsl:template>
</xsl:stylesheet>

我得到了以下的output.xml ,没有评论

<?xml version="1.0" encoding="UTF-8"?>
<test>[Saxonica]
    (2.0
    )[http://www.saxonica.com/]
</test>

以下控制台日志:

JAXP: find factoryId =javax.xml.transform.TransformerFactory
JAXP: found system property, value=net.sf.saxon.TransformerFactoryImpl
JAXP: created new instance of class net.sf.saxon.TransformerFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null
JAXP: find factoryId =javax.xml.parsers.SAXParserFactory
JAXP: loaded from fallback value: com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl
JAXP: created new instance of class com.sun.org.apache.xerces.internal.jaxp.SAXParserFactoryImpl using ClassLoader: null

当没有整个管道运行时,我确实得到了这个:

javax.xml.transform.TransformerFactory tFactory = javax.xml.transform.TransformerFactory.newInstance();
javax.xml.transform.Transformer transformer = tFactory.newTransformer(xsl);
transformer.transform(input, output);

结果

<?xml version="1.0" encoding="UTF-8"?><!--Nice comment -->
<test>[Saxonica]
    (2.0
    )[http://www.saxonica.com/]
</test>

有谁知道为什么JAXP管道会省略注释?

1 个答案:

答案 0 :(得分:1)

SAX2 ContentHandler接口不会收到评论通知。为此你需要一个LexicalHandler。但是SAX2助手类XMLFilterImpl没有实现LexicalHandler,因此它有效地删除了注释。

切换到s9api代替JAXP - 它做得更好。