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

Card Puncher Data Processing

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.





Discover More
HTML /XML - Reserved Character/Word

This page is the reserved word/characters of XML based language. If a reserved character is in : the value of an attribute, it should be first encoded in a entity the node value: in Xml: you...
Card Puncher Data Processing
XML - Character data (CDATA) - Escape

All text that is not markup or comment constitutes the character data of the document (known as CDATA). CDATAPCDATAchild If you want to embedded reserved characters xml character in your document,...



Share this page:
Follow us:
Task Runner