Escape a whole block of text with a CDATA section.
In a previous lesson, we learned how to escape a single character by using an entity reference. In this lesson, we'll look at how to escape a whole block of text — not just a single character.
In XML, a CDATA section is used to escape a block of text that would otherwise be parsed as markup.
Why Are CDATA Sections Useful?
You might occasionally find that your data contains large blocks of text with lots of potentially problematic characters. For example, your data could contain a programming script. Many progamming scripts contain characters such as less than/greater than signs, ampersands etc, which would cause problems for the XML processor.
CDATA allows you to escape the whole block of text. This eliminates the need to go through the whole script, individually replacing all the potentially problematic characters. The XML processor knows to escape all data between the
You declare a CDATA section using
<![CDATA[ as the opening tag, and
]]> as the closing tag.
CDATA Section Example
W3C has recommended that all scripts within an XHTML document be escaped using CDATA sections.