问题
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?
回答1:
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.
回答2:
Consider using Andrew Welch's LexEv tool (bundled I believe with KernowForSaxon), which preprocesses CDATA start and end tags into something different (processing instructions perhaps?) that's visible in the XSLT data model and thus available to the application.
来源:https://stackoverflow.com/questions/13727161/keep-cdata-for-xstl-transformation