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 |
|---|---|---|
< |
< |
less than |
> |
> |
greater than |
& |
& |
ampersand |
" |
" |
quotation mark |
' |
' |
apostrophe |
These five work in every XML document with no declaration:
<examples>
<math>3 < 5 && 5 > 2</math>
<company>Marks & Spencer</company>
<title text="The "Best" Café"/>
<phrase>It'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
,©andé. XML only knows the five above. Writing in XML is an error unless a DTD declares it.
<footer>© 2026 Example Ltd </footer>Character references
Any Unicode character can be written by its code point — decimal &#NNNN; or hexadecimal &#xHHHH;. These always work, with no declaration:
<symbols>
<copyright>© 2026</copyright>
<nbsp>a b</nbsp>
<euro>€12.50</euro>
<heart>❤</heart>
</symbols>| You want | Write |
|---|---|
| © | © or © |
| non-breaking space |   or   |
| € | € or € |
| — (em dash) | — or — |
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:
<!DOCTYPE notice [
<!ENTITY org "Riverside Community Library">
<!ENTITY nbsp " ">
]>
<notice>&org; is closed on public holidays. 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":
<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 keywordCDATAmust 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
<!DOCTYPE html>
<html>
<body>
<script>
const xml = `<pair>
<escaped>if (a < b && 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?
&< '
What sequence ends a CDATA section?
-->]]>?></CDATA>
Where can a CDATA section NOT be used?
- Inside element content
- Inside an attribute value
- Inside the root element
- Around JavaScript code