Löydän usein tämän oudon CDATA
-tagin XML
-tiedostoista:
<![CDATA[some stuff]]>
Olen havainnut, että tämä CDATA
-tagi on aina alussa, ja sitten sitä seuraa jotain muuta.
Mutta joskus sitä käytetään, joskus ei. Oletan, että sen tarkoituksena on merkitä, että jotain tavaraa
on "data", joka lisätään sen jälkeen. Mutta minkälaista dataa on "some stuff"? Eikö kaikki, mitä kirjoitan XML-tunnisteisiin, ole jonkinlaista dataa?
CDATA on lyhenne sanoista Character Data, ja se tarkoittaa, että näiden merkkijonojen välissä oleva data sisältää dataa, joka voisi tulkita XML-merkinnäksi, mutta jota ei pitäisi tulkita.
CDATA:n ja kommenttien keskeiset erot ovat seuraavat:
]]>
(CDEnd
), kun taas kommentissa `--`` ei ole sallittu.Tämä tarkoittaa, että kun otetaan huomioon nämä kolme XML-pätkää yhdestä hyvin muotoillusta asiakirjasta:
<!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>