SCDJWS Study Guide: XML Basic
Some of the characters have special meanings in XML. If an element contains these characters, the parser will be confused. CDATA sections are used to escape blocks of text containing these characters, such as "<", ">", "&" and so on. A CDATA section marks the text as literal so that it will not be parsed, instead be considered just a string of characters. CDATA sections can appear inside element content to allow the special characters to appear.
A CDATA section begins with the character sequence "<![CDATA["and ends with the character sequence "]]>". The text in between the "<![CDATA[" and the "]]>" are escaped.
The following is an example of using CDATA to include HTML content inside an XML element without
changing the HTML "<" and ">" to < and >. Since the
<? xml version="1.0" encoding="UTF-8" ?>
<title>Little Sister, Big Sister</title>
<author>Diana Cain Blutherthal</author>
<tr><td>Chapter Three</td><td>The Chocolate Bar</td><td>....31</td></tr>
<tr><td>Chapter Four</td><td>Thunder Cookies</td><td>....40</td></tr>
All tags and entity references inside these CDATA markers are ignored by the XML parser that treats them just like any character data. In another words, an XML parser ignores all markup characters such as <, >, and & between these CDATA markers. The only markup an XML parser recognizes inside a CDATA section is the closing character sequence �]]>". The character string "]]>� must not appear inside a CDATA block as it would signal the end of the CDATA section. Instead, the closing greater-than character must be escaped using the appropriate entity >. Therefore, CDATA sections cannot be nested.
Keep in mind, though, that nothing inside a CDATA Section is parsed. Therefore, if you were to include entities, they would not be parsed. So, <I> would remain <I> if it were contained inside a CDATA section.
A CDATA Section can be used anywhere PCDATA occurs�as element content, and so on. However, attribute values are always parsed unless they are specified as CDATA in a DTD or Schema. So, you cannot include a CDATA Section in an attribute value.
Comments are not recognized in a CDATA section. The XML parser will treat any "<! � comments - >" in the CDATA block as the literal text without parsing them. CDATA does not work in HEML.