TExcelTAD
Home › XML › XML CDATA & Entities

XML CDATA and Entities

Some characters have a special job in XML: < starts a tag and & starts an entity reference. When you need these characters as text, you have two tools: entity references for a few characters, and CDATA sections for whole blocks.

The five predefined entities

Entity Character Name
&lt; < less than
&gt; > greater than
&amp; & ampersand
&quot; " quotation mark
&apos; ' apostrophe

These five work in every XML document with no declaration:

xml · try it yourself
<examples>
  <math>3 &lt; 5 &amp;&amp; 5 &gt; 2</math>
  <company>Marks &amp; Spencer</company>
  <title text="The &quot;Best&quot; Café"/>
  <phrase>It&apos;s raining</phrase>
</examples>

The element tree printed by the playground shows the decoded text: 3 < 5 && 5 > 2.

Note: HTML has hundreds of named entities like &nbsp;, &copy; and &eacute;. XML only knows the five above. Writing &nbsp; in XML is an error unless a DTD declares it.

xml · try it yourself
<footer>&copy; 2026 Example Ltd&nbsp;</footer>

Character references

Any Unicode character can be written by its code point — decimal &#NNNN; or hexadecimal &#xHHHH;. These always work, with no declaration:

xml · try it yourself
<symbols>
  <copyright>&#169; 2026</copyright>
  <nbsp>a&#160;b</nbsp>
  <euro>&#x20AC;12.50</euro>
  <heart>&#x2764;</heart>
</symbols>
You want Write
© &#169; or &#xA9;
non-breaking space &#160; or &#xA0;
€ &#8364; or &#x20AC;
— (em dash) &#8212; or &#x2014;

Tip: If your file is saved as UTF-8 (the default), you can simply type ©, € or é directly. Character references are mainly useful when your editor or keyboard makes a character hard to enter, or for invisible characters like the non-breaking space.

Custom entities

A DTD can define your own named entities, as shown in the DTD lesson:

xml · try it yourself
<!DOCTYPE notice [
  <!ENTITY org "Riverside Community Library">
  <!ENTITY nbsp "&#160;">
]>
<notice>&org; is closed on public holidays.&nbsp;Thank you!</notice>

CDATA sections

When a block of text contains many special characters — source code, an HTML snippet, a formula — escaping each one is painful and hard to read. A CDATA section tells the parser "everything until ]]> is plain text; don't look for tags or entities":

xml · try it yourself
<snippet language="javascript">
  <![CDATA[
    if (a < b && b > 0) {
      el.innerHTML = "<b>Tom & Jerry</b>";
    }
  ]]>
</snippet>

The text inside is exactly the same as if every < and & had been escaped — CDATA is just a more readable way to write it.

CDATA rules

  • Starts with <![CDATA[ and ends with ]]> — the keyword CDATA must be uppercase.
  • Cannot be nested, and can't contain the sequence ]]> itself. (To include it, split it: ]]]]><![CDATA[>.)
  • Only works inside element content, not inside attribute values.
  • Does not let you include invalid characters (such as raw control characters) — it only switches off markup recognition.

Seeing the difference in the DOM

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<pair>
      <escaped>if (a &lt; b &amp;&amp; c) {}</escaped>
      <cdata><![CDATA[if (a < b && c) {}]]></cdata>
    </pair>`;
    const doc = new DOMParser().parseFromString(xml, 'application/xml');
    const escaped = doc.querySelector('escaped');
    const cdata = doc.querySelector('cdata');

    console.log('escaped text :', escaped.textContent);
    console.log('cdata text   :', cdata.textContent);
    console.log('same text?   :', escaped.textContent === cdata.textContent);
    console.log('node types   :', escaped.firstChild.nodeName, 'vs', cdata.firstChild.nodeName);
  </script>
</body>
</html>

Both elements hold the same text. The only difference is the node type: an ordinary text node (#text) versus a CDATA section node (#cdata-section).

Which should I use?

Situation Use
An occasional & or < in normal text Predefined entities
A character that's hard to type A character reference, or just type it in UTF-8
Repeated boilerplate text in your own format A DTD entity
A block of code or markup stored as text CDATA
Text inside an attribute value Entities (CDATA isn't allowed there)

Test yourself

Which entity is NOT predefined in XML?

  1. &amp;
  2. &lt;
  3. &nbsp;
  4. &apos;

What sequence ends a CDATA section?

  1. -->
  2. ]]>
  3. ?>
  4. </CDATA>

Where can a CDATA section NOT be used?

  1. Inside element content
  2. Inside an attribute value
  3. Inside the root element
  4. Around JavaScript code