Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is CDATA and what does it do?

I sometimes notice a CSS or JS code surrounded with <![CDATA[ and ]]> tags. I couldn't find any explanation to this. What does CDATA do? Why is it used by some people and what is it needed for?

like image 988
rhino Avatar asked Sep 28 '10 14:09

rhino


People also ask

What is the purpose of CDATA?

A CDATA section is used to mark a section of an XML document, so that the XML parser interprets it only as character data, and not as markup. It comes handy when one XML data need to be embedded within another XML document.

Is CDATA necessary?

CDATA is necessary in any XML dialect, because text within an XML node is treated as a child element before being evaluated as JavaScript.

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 CDATA CSS?

When you write inline javascript or CSS in an HTML file (using the script and style tags), Those CDATA comments prevent things like the HTML validators from catching false positives when validating an HTML document. Copy link CC BY-SA 2.5. Follow this answer to receive notifications.


1 Answers

It tells the interpreter that it should not try to interpret the data enclosed in the tags. For example, if you want a XML file to contain a comment with < or >, XML interpreters will report the file as invalid because the caracters < and > will not be part of standard tag. You simply have to surround the code with the CDATA tags.

like image 118
Philippe Carriere Avatar answered Sep 29 '22 20:09

Philippe Carriere