XML CDATA Sections — When and How to Use Them
Understand XML CDATA sections for embedding raw text, HTML, or code without escaping. Learn CDATA syntax, nesting limitations, formatting considerations, and when to use CDATA vs. entity escaping.
Detailed Explanation
XML CDATA Sections
CDATA (Character Data) sections allow you to include text that would otherwise need escaping in XML. Content inside a CDATA section is treated as literal text — the XML parser does not interpret it as markup.
CDATA Syntax
<script>
<![CDATA[
function compare(a, b) {
if (a < b && b > 0) {
return a & b;
}
}
]]>
</script>
Without CDATA, the <, >, and & characters would need to be escaped as <, >, and &.
CDATA vs. Entity Escaping
| Approach | Pros | Cons |
|---|---|---|
| CDATA | Readable for code/HTML content | Cannot contain ]]> literal |
| Entity escaping | Works everywhere | Hard to read for code blocks |
Use CDATA when:
- Embedding HTML, JavaScript, SQL, or other code in XML
- The content contains many special characters
- Human readability of the embedded content matters
Use entity escaping when:
- The text is short with few special characters
- The content might contain the literal string
]]> - You need the content to be processed by the XML parser
CDATA Nesting Limitation
CDATA sections cannot be nested. The sequence ]]> always ends the current CDATA section, regardless of context:
<!-- ERROR: premature CDATA end -->
<![CDATA[
Some text containing ]]> breaks here
]]>
The workaround is to split the content:
<![CDATA[Some text containing ]]]]><![CDATA[> continues here]]>
Formatting CDATA Content
A good XML formatter preserves CDATA content exactly as written. The content inside CDATA should not be re-indented or modified, since it represents literal text where whitespace may be significant.
CDATA in Common Use Cases
- RSS feeds:
<content:encoded>often wraps HTML in CDATA - SOAP messages: Embedded XML or HTML payloads
- Configuration files: SQL queries, script blocks, template strings
- Data export: CSV or JSON data embedded in XML elements
Use Case
CDATA handling is essential when working with content management systems that store HTML within XML feeds, build systems that embed scripts in configuration files, data migration tools that transport code or markup inside XML containers, and RSS/Atom feed generators that include full article HTML.