What is a CDATA Sections in XML ? (and HTML compliancy)


A cdata section 1) delimits a text as being cdata (character data). It's used mostly to be able to use (ie escape) reserved xml characters that would otherwise be recognized as xml markup.


An example of a CDATA section, in which <greeting> and </greeting> are recognized as character data, not xml markup:

<![CDATA[<greeting>Hello, world!</greeting>]]> 

HTML and XHTML compliancy

CDATA works only in strict XML language, therefore, if you want your document to be correctly parsed :

you have the following solutions:

Entity encoding

You can encode the reserved character with an HTML entity.

For instance, the above example would become:

&#x3C;greeting&#x3E;Hello, world!&#x3C;/greeting&#x3E;

where every reserved character was transformed as an entity. For instance, the less than character was encoded as &#x3C;.

The above entity syntax is then rendered correctly in HTML as seen below:

Commenting out cdata delimiters

A common solution is to comment out, the CDATA delimiters symbols.

For instance:

  • In Javascript:
document.write("< is greater than >");
  • Or in css:
<style type="text/css">
body { background-image: url("illustration.png?width=1200&height=600") }     


CDATA sections:

  • begin with the string <![CDATA[
  • and end with the string ]]>

CDATA sections may occur anywhere character data may occur.

Powered by ComboStrap