Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - XSLT Developer's Guide

Preserving Markup Characters by Using CDATA Sections

CDATA sections within an XML document can contain markup elements common to XML, without affecting whether the document is well-formed. For example, if you want to include a mathematical XML application using the < and > characters to refer to less-than and greater-than, you can use a CDATA section.

To specify that a given element in the result tree will contain a CDATA section, include the element name as a value of the cdata-section-elements attribute of the <xsl:output> element. The CDATA section created in this way will preserve white space and character entities.

Example

The XSLT processor scans the result tree, making a CDATA section of each item in the cdata-section-elements attribute.

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.

XML File (cdata.xml)

<?xml version='1.0'?>
<?xml-stylesheet type="text/xsl" href="cdata.xsl" ?>
<document>
<scriptcode language="JScript">
function message(msg){
alert(msg);
}
</scriptcode>
</document>

XSLT File (cdata.xsl)

<?xml version='1.0'?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://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>

Formatted Output

None (blank).

Processor Output

<?xml version="1.0" encoding="UTF-16"?><document><script language="JScript"><![CDATA[
function message(msg){
alert(msg);
}
]]></script></document>

See Also

CDATA Sections