In general, serialization options are ignored when Saxon is not doing the serializing. When you send the result tree to an XMLStreamWriter then it is the XMLStreamWriter that does the serializing, not Saxon.

It's true enough that some serialization options (e.g. indent and cdata-section-elements) could be implemented by Saxon in such circumstances, but I think it's much clearer to say that either Saxon does all the serializing (at least as far as a character stream if not a byte stream), or it does none.

Michael Kay
Saxonica


On 4 Feb 2014, at 21:31, Archie Cobbs <archie@dellroad.org> wrote:

It appears that cdata-sectioelements is being ignored when an XMLStreamWriter is used with Saxon. This is Saxon-HE version 9.5.1.4.

Here's a simple test case:

foo.xsl:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:fn="http://www.w3.org/2005/xpath-functions"
  version="2.0" exclude-result-prefixes="fn">

    <xsl:output method="xml" indent="yes" encoding="UTF-8" cdata-section-elements="foo"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

</xsl:transform>

foo.xml:

<docelem>
    <foo>this</foo>
    <bar>that</bar>
</docelem>

xx.java:

import java.io.*;
import java.net.*;
import javax.xml.stream.*;
import javax.xml.transform.*;
import javax.xml.transform.stax.*;
import javax.xml.transform.stream.*;

public class xx {

    public static final String SAXON_TRANSFORMER_FACTORY_CLASS_NAME = "net.sf.saxon.TransformerFactoryImpl";

    public static Transformer getTransformer(String resourceName) throws IOException, TransformerException {
        final TransformerFactory transformerFactory = xx.getTransformerFactory(SAXON_TRANSFORMER_FACTORY_CLASS_NAME);
        final URL url = xx.class.getResource(resourceName);
        final InputStream input = url.openStream();
        final Transformer transformer = transformerFactory.newTransformer(new StreamSource(input, url.toString()));
        //transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        //transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "foo");
        //transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        input.close();
        return transformer;
    }

    private static TransformerFactory getTransformerFactory(String className) {
        final TransformerFactory transformerFactory = TransformerFactory.newInstance(
          className, Thread.currentThread().getContextClassLoader());
        return transformerFactory;
    }

    protected void normalize(String xslResource, InputStream input, OutputStream output) throws IOException, XMLStreamException, TransformerException {
        final XMLStreamWriter streamWriter = XMLOutputFactory.newInstance().createXMLStreamWriter(output, "UTF-8");
        xx.getTransformer(xslResource).transform(new StreamSource(input), new StAXResult(streamWriter));
    }

    public static void main(String[] args) throws Exception {
        new xx().normalize(args[0], System.in, System.out);
        System.out.println();
    }
}

Test case output:

$ saxon9 -s:foo.xml -xsl:foo.xsl
<?xml version="1.0" encoding="UTF-8"?>
<docelem>
      <foo><![CDATA[this]]></foo>
      <bar>that</bar>
</docelem>

$ java xx foo.xsl < foo.xml
<?xml version="1.0" ?><docelem>
    <foo>this</foo>
    <bar>that</bar>
</docelem>

I expected the <foo> element to contain CDATA in the second case as well. It doesn't help to uncomment the OutputKeys lines either.

Thanks,
-Archie

--
Archie L. Cobbs
------------------------------------------------------------------------------
Managing the Performance of Cloud-Based Applications
Take advantage of what the Cloud has to offer - Avoid Common Pitfalls.
Read the Whitepaper.
http://pubads.g.doubleclick.net/gampad/clk?id=121051231&iu=/4140/ostg.clktrk_______________________________________________
saxon-help mailing list archived at http://saxon.markmail.org/
saxon-help@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/saxon-help