SyntaxStudy
Sign Up
XML CDATA Sections and Comments
XML Beginner 1 min read

CDATA Sections and Comments

A CDATA (Character Data) section is a way to include text in XML that contains characters that would otherwise need to be escaped. Everything inside a CDATA section is treated as raw character data by the XML parser — the less-than sign, ampersand, and other reserved characters require no escaping. A CDATA section begins with . The only string that cannot appear inside a CDATA section is ]]>, which marks its end. CDATA sections are commonly used when embedding code snippets, SQL queries, HTML fragments, or mathematical formulas in XML. They make the document more readable by avoiding dense entity reference sequences. However, CDATA sections are still part of the document's text node — a DOM parser will expose their content as a CDATASection node or merge it into a Text node depending on the API. XML comments use the same syntax as HTML comments: . The string -- (double hyphen) is not allowed inside a comment. Comments are ignored by the XML parser and do not appear in the parsed document tree. Processing instructions, written as , pass instructions to the application processing the XML and are preserved in the document tree unlike comments.
Example
<?xml version="1.0" encoding="UTF-8"?>
<snippets>

    <!-- This is an XML comment. Double-hyphen (--) is not allowed inside. -->

    <!-- CDATA section: embed HTML without escaping -->
    <htmlFragment><![CDATA[
        <div class="box">
            <p>Hello <strong>World</strong></p>
            <a href="http://example.com?a=1&b=2">Link</a>
        </div>
    ]]></htmlFragment>

    <!-- CDATA section: embed SQL -->
    <query><![CDATA[
        SELECT id, name
        FROM users
        WHERE age > 18 AND status = 'active'
        ORDER BY name ASC;
    ]]></query>

    <!-- Processing instruction targeting a stylesheet -->
    <?xml-stylesheet type="text/css" href="style.css"?>

    <!-- Regular text needs entity references -->
    <escaped>if (a &lt; b &amp;&amp; c &gt; 0) { }</escaped>

</snippets>