Is there a way to transform cdata sections of an XML document using an XSL template?
2 answers
Read this article - CDATA Sections
SUMMARY: In an XSLT stylesheet, the CDATA section is just a utility to stop you from having to run '<' etc. The target you are targeting is to copy something you have in your XML source right into your HTML Output. The xsl: copy-of element is there for just that purpose. xsl: copy will give an exact copy of what you choose, including attributes and content.
XML document.
<?xml version="1.0" encoding="iso-8859-1"?>
<know>
<title/>
<topic title="" href="">
<![CDATA[
Text
]]>
</p>
</topic>
</know>
xsl Document.
<xsl:stylesheet
version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:template match="know">
<xsl:value-of select="title"/>
<xsl:for-each select="topic">
<xsl:value-of select="@title"/>
<xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
+3
source to share
XSLT treats CDATA sections as plain text, so you can treat them the same way as text nodes. Note that XSLT does not keep CDATA sections separate from surrounding text. Thus, if you have
<foo>bar <![CDATA[baz]]> qux</foo>
The original tree will be
- Document
- Element: foo
- Text: "bar baz qux"
- Element: foo
+1
source to share