Table of Contents

About

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.

Example

An example of a CDATA section, in which

and

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:
<script>
//<![CDATA[
document.write("< is greater than >");
//]]>
</script>
  • Or in css:
<style type="text/css">
/*<![CDATA[*/
body { background-image: url("illustration.png?width=1200&height=600") }     
/*]]>*/
</style>

Syntax

CDATA sections:

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

CDATA sections may occur anywhere character data may occur.