Updated April 5, 2023
Introduction to XML CDATA
XML Character data (CDATA) is defined as Blocks of texts and a type of XML Node recognized by the mark-up languages but are not parsed by the parsers. This is used to solve the inclusion of the mathematical term in the XML document. To pass a math equation <,> CDATA is used to include in the code section. DATA is meant only for the group of text specifying mark-up like characters. The CDATA section includes all the mark-up characters exactly what they are when they are passed it to the application and eliminates nesting. In simple terms, all the entity references and XML tags are excluded by the processor while parsing and treated as character data.
Syntax:
Given below is the CDATA syntax:
<!CDATA[
// CDATA CODE with characters
]]>
In the above syntax the first section “<!” starts with the delimiters and ended up with “ ]]> “ just wrapping up with the CDATA tag, the characters in the code sections are treated as only text not as mark-up which may also include special characters. The XML mark-up is embedded which are not interpreted. Any content that is declared inside the<! [CDATA[ and ]]> is ignored by the XML Processor and almost it is equivalent to the comment section in XML.
How CDATA works in XML?
Some reserved characters in PCDATA are not allowed in the XML document during parsing which would give an error in the browser. To get around this we use CDATA section, inherited from SGML. While Executing the file a node with CDATA keyword section is parsed, the parser skips the inner data of Sections. And this special node views like a cross between an element and text also includes opening and closing tags where the text could be multi-lined. The aim objective is to include XML fragments.
Let’s take a sample of an XML document:
[CDATA("most existing feature<BR> ability to inherit") providing XML- data with standards:
<![CDATA[most existing feature<BR> ability to inherit]]>
Here the characters are formatted with CDATA. For CDATA,the character entity is restricted as it generates issues when exporting data with dataset appearing like processing instructions.
The following is invalid as CDATA is nested:
<![CDATA[Nesting is not allowed "]]>" document]]>
So, the above line is written as two parts as follows. This is the exact way to put a section end inside a CDATA section. By which replace]] > in the specified data with]]]] ><! [CDATA [> so that “>” is perfectly away from the brackets (as we broke the bracket sequence). And when the parser looks out for ]]> it is specified as the three-character sequence.
Now let’s see the document which is listed below:
<![CDATA[Nesting is not allowed "]]]]><![CDATA[>" document.]]>
They are helpful when we need to embed another xml document in the existing xml document.
The sample is given below:
<?xml version="1.0" encoding="utf-8"?>
<boarding>
<flight>Iternational</flight>
<Flight name><![CDATA[<?xml version="1.0" encoding="utf-8"?><boarding>…</boarding>]]></Flight name>
</boarding>
Though with certain benefits of CDATA, clearly it has some limitations too, CDATA does go well with html and XHTML. There are few possibilities in losing the contents. Since CDATA cannot be nested section end is made more special. Last point is size of the section that is important while transferring XML file with large amount of information.
Examples of XML CDATA
Given below are the examples mentioned:
Example #1
Enclosing the character entities with CDATA in xml file.
Code:
cdata.xml
<?xml version="1.0" encoding="UTF-8"?>
<SoftwareEngineer>
<empl id="01">
<name>
<projectname> Man-router</projectname>
<Workingdomain> machine learning</Workingdomain>
</name>
<Enddate>
<entities><![CDATA[
This is the local project with the fibre optics.
All the statistical manipulation is performed. Example. '"&<> and submission date 12/12/2020
]]></entities>
</Enddate>
</empl>
</SoftwareEngineer>
So in the Result output, there is a white space after the CDATA sections.
Output:
Example #2
CDATA with mathematical expression to be included in the XML file.
Code:
cdata.xml
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title> Displaying the Mathematical function </title>
</head>
<body>
<h1><b> This example Shows how to display a CDATA when Special characters are included </b></h1>
<div id ="Demo">
<![CDATA[
The statement of the formula includes " if Q(a,b) is a b < c && a > c "
]]>
</div>
</body>
</html>
Output:
Example #3
HTML file inside CDATA part.
Code:
har.xml
<HTML-Doc>
<tag>
<tname>B</tname>
<description>Bold</description>
<demo><![CDATA[
<B>Bold the letters in the content <EM>Next</EM>part.</B>
]]></demo>
</tag>
<tagtwo>
<tname>Element</tname>
<description>Doc Type Element</description>
<demo><![CDATA[
<HTML>
<HEAD><h1>Sample XML Code</h1></HEAD>
<BODY>
<i>Make a paragraph change</i>
</BODY>/HTML>
]]></demo>
</tagtwo>
<!--more tags to follow...-->
</HTML-Doc>
Output:
Example #4
Embedding JavaScript or any style sheets in CDATA section.
Probably if suppose there is a situation in including JavaScript in XML code then here is the solution by imposing them in CDATA. The “<” and “>” are likely to be wiped by the equivalents < and >.
Code:
func.xml
<?xml version="1.0" encoding="UTF-8"?>
<!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/TR/xhtml1" xml:lang="en" lang="en">
<head>
<h1>Cdata in Xhtml- demo</h1>
</head>
<body>
<script type="text/JavaScript">
<![CDATA[
function open( x, y, z ) {
if( x< y && y > z ) {
return y--;
}
return 0;
}
]]>
</script>
</body>
</html>
Output:
Example #5
Code:
func.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="application/xhtml+xml;charset=utf-8"/>
<title>XHTML Page- CDATA</title>
</head>
<body>
<h2>XHTML Page- CDATA</h2>
<p> I think java would dominate the Programming Landscape<em>effectively</em>
java.</p>
<p>
java version:
</p>
<p id="1">
<p> I think java would dominate the Programming Landscape<em>effectively</em>
java.</p>
</p>
<p>
CDATA Sectionpart:
</p>
<p id="2">
well,
<![CDATA[<p> I think java would dominate the Programming Landscape<em>effectively</em>
java.</p>
]]>
in current trends?
</p>
</body>
</html>
Output:
Conclusion
Therefore, in this article, we have seen what is CDATA all about and have analysed how to escape special characters while using in the CDATA section. It has many business impacts when the tags are used improperly, but has good performance when utilized properly. And CDATA is been used when they are need of large amount of texts to appear in the XML documents. The overall summary is while using CDATA to embed mark-up in XML document benefits us from having to encode the data, care should be taken with the effect of the client-server communication.
Recommended Articles
This is a guide to XML CDATA. Here we discuss the introduction to XML CDATA, how it works in XML along with examples respectively. You may also have a look at the following articles to learn more –