The role of XML CDATA

Time:2019-12-10

This is using XML CDATA
All text in an XML document is parsed by the parser.
Only text within CDATA parts is ignored by the parser.

Analytical data
XML parsers typically process all the text in an XML document.
When an XML element is parsed, the text inside the XML element is also parsed:
<message>This text is also parsed</message>
The reason why the XML parser does this is that there may be other elements inside the XML element. Like the following example, the name element contains the first and last elements inside:
<name><first>Bill</first><last>Gates</last></name>
The parser will think that the above code is as follows:
<name>
<first>Bill</first>
<last>Gates</last>
</name>

Escape character
Illegal XML characters must be replaced with corresponding entities.
If you use characters like “<” in an XML document, the parser will make an error because it will think that this is the beginning of a new element. So you shouldn’t write code like this:
<message>if salary < 1000 then</message>
To avoid this, you must convert the character “<” to an entity, as follows:
<message>if salary < 1000 then</message>
Here are five predefined entities in an XML document:
< less than sign
>> greater than sign
& & amp;
” single quote
“” double quotes
Entities must begin with the symbol “&” and end with the symbol “;”.
Note: only “<” and “&” characters are strictly prohibited for XML. The rest is legal. In order to reduce errors, it is a good habit to use entities.

CDATA components
Everything inside CDATA is ignored by the parser.
If the text contains a lot of “<” and “&” characters – just like the program code, it’s best to put them all in the CDATA widget.
A CDATA part starts with “<! [CDATA [” and ends with “]] >
<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1
}
else
{
return 0
}
}
]]>
</script>
In the previous example, all text between CDATA parts is ignored by the parser.
CDATA notes:
CDATA parts can no longer be included (cannot be nested) between CDATA parts. If the CDATA part contains the characters “]] > or” <! [CDATA [“, it is likely to make an error.
Also note that there are no spaces or line breaks between the strings “]] >.