Friday, May 30, 2008

XML CDATA and PCDATA Sections

Hi All,

I would like to bring a point on table about CDADA and PCDATA sections in XML Document.

PCDATA - Parsed Character Data


XML parsers normally parse all the text in an XML document.
When an XML element is parsed, the text between the XML tags is also parsed:
<message>This text is also parsed</message>
The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):
<name><first>Bill</first><last>Gates</last></name>

Parsed Character Data (PCDATA) is a term used about text data that will be parsed by the XML parser.

CDATA - (Unparsed) Character Data


The term CDATA is used about text data that should not be parsed by the XML parser.

Characters like "<" and "&" are illegal in XML elements.

"<" will generate an error because the parser interprets it as the start of a new element.

"&" will generate an error because the parser interprets it as the start of an character entity.

Some text, like JavaScript code, contains a lot of "<" or "&" characters. To avoid errors script code can be defined as CDATA.

Everything inside a CDATA section is ignored by the parser.

A CDATA section starts with "<![CDATA[" and ends with "]]>":

<script>
<![CDATA[
function GreaterThan(a)
{
if (a > 50) then
{
return 1;
}
else
{
return 0;
}
}
]]>
</script>

In the example above, everything inside the CDATA section is ignored by the parser.

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.

Lets have a simple scenario why we need this or where this can be useful.
Lets say you are developing a page or control for a list and you have some handy static logic that you want to fix in that.
Lets take an simple example saying that the value you pass in textbox can not greater than 50.
So what we can do is that use CDATA secion in XML with any tag. All you need to do is write you entire javascript in XML tag in CDATA secion. Fetch that node in Code using XMLDocument or any other XML class method.
and use Server side registration for client script. here your script will be coming from XML, so whenever you want to change the script, all you have to do is just change the XML and you are done with that.
I hope that sounds good and you got a good idea about how to use CDATA secion and use it for futire use.

Lets say you are developing a page or control for a list and you have some handy static logic that you want to fix in that.

Lets take an simple example saying that the value you pass in textbox can not greater than 50.

So what we can do is that use CDATA secion in XML with any tag. All you need to do is write you entire javascript in XML tag in CDATA secion. Fetch that node in Code using XMLDocument or any other XML class method.

and use Server side registration for client script. here your script will be coming from XML, so whenever you want to change the script, all you have to do is just change the XML and you are done with that.

I hope that sounds good and you got a good idea about how to use CDATA secion and use it for futire use.

Click here to refer more about XML

No comments:




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com