CDATA Sections

A CDATA section, <![CDATA[ ... ]]>, marks a block of text in which < and & are ordinary characters, so you can embed an HTML fragment or a code sample without escaping each one. Sections cannot nest, and the only sequence they cannot contain is their own terminator, ]]>. The data model has no CDATA node: the section is just a convenient way to write text, as Saxon 726,956 shows:

CDATA is only a way of writing text: the parsed value is the same
printf '<blurb><![CDATA[<p>Recipes from <b>Kerala</b> & Zanzibar</p>]]></blurb>\n' > blurb.xml
xquery -s:blurb.xml -qs:'string(/blurb)'
xquery -s:blurb.xml -qs:'/blurb'
xquery -s:blurb.xml -qs:'/blurb' '!cdata-section-elements=blurb'
Output
"<p>Recipes from <b>Kerala</b> & Zanzibar</p>"
<blurb>&lt;p&gt;Recipes from &lt;b&gt;Kerala&lt;/b&gt; &amp; Zanzibar&lt;/p&gt;</blurb>
<blurb><![CDATA[<p>Recipes from <b>Kerala</b> & Zanzibar</p>]]></blurb>

The string value holds the tags as plain text. Serialized again, the default output escapes them, which is equivalent; ask for cdata-section-elements if a downstream consumer insists on CDATA. To include ]]> itself, end the section and start another: <![CDATA[a]]]]><![CDATA[>b]]>. CDATA does not make binary data safe, since XML forbids most control characters; send files as Base64 text instead.


CDATA

<![CDATA[……]]> denotes a section of character data. This is used to escape blocks of text containing characters that would otherwise be recognized as markup. In the example in Markup Components, <old_price> is recognized as character data, not markup. CDATA sections can contain the < and & literals. They cannot be nested.
ch02-cdata-example.xmlXML
<?xml version="1.0" encoding="UTF-8"?>
<description>
   <![CDATA[<old_price>30.00</old_price>]]>
</description>