Monday, July 4, 2011

XML CDATA

XML CDATA

All text in an XML document will be parsed by the parser.

But text inside a CDATA section will be ignored by the parser.

Why Are CDATA Sections Useful?

You might occasionally find that your data contains large blocks of text with lots of potentially problematic characters. For example, your data could contain a programming script. Many progamming scripts contain characters such as less than/greater than signs, ampersands etc, which would cause problems for the XML processor.

CDATA allows you to escape the whole block of text. This eliminates the need to go through the whole script, individually replacing all the potentially problematic characters. The XML processor knows to escape all data between the CDATA tags.
CDATA Syntax

You declare a CDATA section using as the closing tag.

Example:


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


PLEASE Remove Firstly All Asterics *


CDATA Section Example

In this example, we have a block of JavaScript code inside an XHTML document. If you've been involved in web development, you'll probably know how common JavaScript is on the web. You might also know that any block of JavaScript code could contain all sorts of potentially problematic characters.

All scripts within an XML document be escaped using CDATA sections.


<*?xml version="1.0"?>
<*!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<*html xmlns="http://www.w3.org/1999/xhtml">
<*head>
<*title>Displaying the Time<*/title>

<*script type="text/javascript">
<*![CDATA[
*var currentTime = new Date()
*var hours = currentTime.getHours()
*var minutes = currentTime.getMinutes()

*var suffix = "AM";
*if (hours >= 12) {
suffix = "PM";
hours = hours - 12;
}
*if (hours == 0) {
hours = 12;
}

*if (minutes < 10)
minutes = "0" + minutes

*document.write("" + hours + ":" + minutes + " " + suffix + "")
]]>
<*/script>

<*/head>
<*body>
<*h1>Displaying the Time<*/h1>
<*/body>
<*/html>


PLEASE Remove Firstly All Asterics *


Notes on CDATA sections:

A CDATA section cannot contain the string "]]>". Nested CDATA sections are not allowed.

The "]]>" that marks the end of the CDATA section cannot contain spaces or line breaks.

THANK YOU