Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does <![CDATA[]]> in XML mean?

Tags:

xml

cdata

I often find this strange CDATA tag in XML files:

<![CDATA[some stuff]]> 

I have observed that this CDATA tag always comes at the beginning, and then followed by some stuff.

But sometimes it is used, sometimes it is not. I assume it is to mark that some stuff is the "data" that will be inserted after that. But what kind of data is some stuff? Isn't anything I write in XML tags some sort of data?

like image 907
dontWatchMyProfile Avatar asked May 06 '10 20:05

dontWatchMyProfile


People also ask

WHAT IS <![ CDATA in XML?

CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be. The key differences between CDATA and comments are: As Richard points out, CDATA is still part of the document, while a comment is not.

What does CDATA mean in XML?

The term CDATA, meaning character data, is used for distinct, but related, purposes in the markup languages SGML and XML. The term indicates that a certain portion of the document is general character data, rather than non-character data or character data with a more specific, limited structure.

How do I use CDATA in XML?

A CDATA section begins with the character sequence <! [CDATA[ and ends with the character sequence ]]>. Between the two character sequences, an XML processor ignores all markup characters such as <, >, and &. The only markup an XML pro-cessor recognizes inside a CDATA section is the closing character sequence ]>.

What is a CDATA error?

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.


1 Answers

CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be.

The key differences between CDATA and comments are:

  • As Richard points out, CDATA is still part of the document, while a comment is not.
  • In CDATA you cannot include the string ]]> (CDEnd), while in a comment -- is invalid.
  • Parameter Entity references are not recognized inside of comments.

This means given these four snippets of XML from one well-formed document:

<!ENTITY MyParamEntity "Has been expanded"> 

<!-- Within this comment I can use ]]> and other reserved characters like < &, ', and ", but %MyParamEntity; will not be expanded (if I retrieve the text of this node it will contain %MyParamEntity; and not "Has been expanded") and I can't place two dashes next to each other. --> 

<![CDATA[ Within this Character Data block I can use double dashes as much as I want (along with <, &, ', and ") *and* %MyParamEntity; will be expanded to the text "Has been expanded" ... however, I can't use the CEND sequence. If I need to use CEND I must escape one of the brackets or the greater-than sign using concatenated CDATA sections. ]]> 

<description>An example of escaped CENDs</description> <!-- This text contains a CEND ]]> --> <!-- In this first case we put the ]] at the end of the first CDATA block      and the > in the second CDATA block --> <data><![CDATA[This text contains a CEND ]]]]><![CDATA[>]]></data> <!-- In this second case we put a ] at the end of the first CDATA block      and the ]> in the second CDATA block --> <alternative><![CDATA[This text contains a CEND ]]]><![CDATA[]>]]></alternative> 
like image 107
Sean Vieira Avatar answered Oct 12 '22 09:10

Sean Vieira