membersound membersound - 4 months ago 27
Java Question

Keep CDATA for xstl transformation?

I'm doing a xstl transformation with saxon from an XML document.
The doc is not standard-valid XML, and I want to preserve all

<![CDATA[<
elements that are found in there.

However using the .xsl file for transformation with

Transformer trans = TransformerFactory.newInstance().newTransformer(new StreamSource(new File("foo.xsl"));
trans.transform(new StreamSource(new File("foo.xml"), new StreamResult(new File("output.xml")));


results in stripping out these CDATA entries. How can I prevent this?

Answer

You can't, as the distinction whether a text originated from a cdata section is not available in the datamodel used by xslt. You can however define in your stylesheet that certain result elements are to be wrapped inside cdata. This is done using the cdata-section-elements attribute of the xsl:output element in your stylesheet.