Creating CDATA Sections
A version of this page is also available for
4/8/2010
CDATA sections allow you to use sequences of characters within an XML stream that contain markup elements common to XML without violating XML well-formed constraints. For example, a mathematical XML application using < and > characters to refer to less-than and greater-than can use a CDATA section.
You can specify that a given element in the result tree will contain a CDATA section by including it in the cdata-section-elements attribute of the <xsl:output> element. The CDATA section created in this way will preserve white space and character entities.
The following is an XML fragment.
<document>
<scriptcode language="JScript">
function message(msg)
{
alert(msg);
}
</scriptcode>
</document>
The following style sheet will be applied to an XML document containing the preceding fragment.
<xsl:stylesheet version="1.0"
xmlns:xsl="https://www.w3.org/1999/XSL/Transform">
<xsl:output cdata-section-elements="script/>
<xsl:template match="scriptcode">
<document>
<script language="JScript">
<xsl:value-of select="."/>
</script>
</document>
</xsl:template>
</xsl:stylesheet>
The XSL Transformations (XSLT) processor scans the result tree, making a CDATA section of each item in the cdata-section-elements attribute.
<document>
<script language="Javascript"><![CDATA[
function message(msg)
{
alert(msg);
}
]]</script>
</document>
Note
To create the CDATA section, the elements listed as values of the cdata-section-elements attribute must be elements in the result tree, not the source tree. In the preceding examples, the Microsoft JScript® code appears as a text node within the source tree's <scriptcode> element, but the cdata-section-elements attribute refers to and affects <script> elements in the result tree.
HTML does not recognize CDATA sections. Do not use this option when generating HTML.