This website may use cookies. More info. That's Fine x
Welcome Login

Cdata Sections


Xml

CDATA  Sections are used to escape a whole block of text.

The XML processor knows to escape all data between the CDATA tags.

You declare a CDATA section using <![CDATA[ as the opening tag, and ]]> as the closing tag.

Compare with entity reference, escaping a single character.

 

Syntax:

<root>
<child>
<![CDATA[
Text you want to escape goes here...
]]>

</child>
</root>

 

Cdata in Program output:

Since it is useful to be able to use less-than signs (<) and ampersands (&) in web page scripts, and to a lesser extent styles, without having to remember to escape them, it is common to use CDATA markers around the text of inline <script> and <style> elements in XHTML documents. But so that the document can also be parsed by HTML parsers, which do not recognise the CDATA markers, the CDATA markers are usually commented-out.

Eg: cdata in script output.  CDATA markers are usually commented-out in js.

    
<script type="text/javascript">
// <![CDATA[

function printAlert {
alert("hello world!");
}

// ]]>
</script>

 

Eg2: in css. Here too, cdata markers are commented out.

<style type="text/css">
/*<![CDATA[*/

body { background-image: url("marble.png?width=300&height=300") }

/*]]>*/
</style>

Created on: Wednesday, April 20, 2011 by Andrew Sin