CDATA blocks have been provided as a convenience measure when you want to include large blocks of special characters as character data, but you do not want to have to use entity references all the time. CDATA sections are used to escape blocks of text containing characters, which would otherwise be recognized as markup. Whatever written inside CDATA section will be ignored by the XML parser. All tags and entity references are ignored by an XML processor that treats them just like any character data. CDATA means Character Data and XML Parsers ignore anything enclosed within CDATA section.
Remember the parser ignores everything inside a CDATA section.
Uses of CDATA sections
Character data is character data, regardless of whether it is expressed via a CDATA section or ordinary markup. New authors of XML documents often misunderstand the purpose of a CDATA section, mistakenly believing that its purpose is to “protect” data from being treated as ordinary character data during processing. Some APIs for working with XML documents do offer options for independent access to CDATA sections, but such options exist above and beyond the normal requirements of XMLprocessing systems, and still do not change the implicit meaning of the data. CDATA is not for odinary text or character data, it should be used where your data contains special characters, such as if you want to enclose a Javascript code in your XML file. This kind of data is bound to generate error while parsing, so you should use CDATA section in such conditions.
Remember, a CDATA section cannot contain the string “]] >” and therefore it is not possible for a CDATA section to contain nested CDATA sections.CDATA sections are useful for writing XML code as text data within an XML document. For example, if one wishes to typeset a book with XSL explaining the use of an XML application, the XML markup to appear in the book itself will be written in the source file in a CDATA section.
For example, to encode “]] >” one would write:
var salary;
function showSalary(amount)
if (amount<=5000) then
]]> </script>
Scroll to Top