Keep CDATA for xstl transformation?

怎甘沉沦 提交于 2019-12-13 04:16:56

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!