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

On 4 Feb 2014, at 21:31, Archie Cobbs <> wrote:

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

Here's a simple test case:


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

<xsl:transform xmlns:xsl=""
  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:apply-templates select="@*|node()"/>




import javax.xml.transform.*;
import javax.xml.transform.stax.*;

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");
        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.out);

Test case output:

$ saxon9 -s:foo.xml -xsl:foo.xsl
<?xml version="1.0" encoding="UTF-8"?>

$ java xx foo.xsl < foo.xml
<?xml version="1.0" ?><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.


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.
saxon-help mailing list archived at