0

When using JAXP pipelin (using Saxon HE), the comments created with don't appear in the resulted .xml.

First I set the system properties to get info and use Saxon and define the input/output:

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

Then I have the following construction with dummy Pre and Post filters:

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);

When run with the following stylesheet:

<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>

I get the follwing output.xml without the comment:

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

And the following console log:

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

When run without the whole pipeline, I do get the coment:

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

results in

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

Does anyone know why the JAXP pipeline omits the comments?

user1971430
  • 77
  • 1
  • 8

1 Answers1

1

The SAX2 ContentHandler interface does not receive notification of comments. For that you need a LexicalHandler. But the SAX2 helper class XMLFilterImpl does not implement LexicalHandler, so it effectively drops the comments.

Switch to s9api in place of JAXP - it does these things much better.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164