Jeg finder ofte dette mærkelige CDATA
-tag i XML
-filer:
<![CDATA[some stuff]]>
Jeg har observeret, at dette CDATA
tag altid kommer i begyndelsen, og derefter efterfulgt af nogle ting.
Men nogle gange bruges det, andre gange ikke. Jeg går ud fra, at det er for at markere, at some stuff
er de "data" der vil blive indsat bagefter. Men hvilken slags data er noget stads
? Er alt, hvad jeg skriver i XML-tags, ikke en slags data?
CDATA står for Character Data og betyder, at dataene mellem disse strenge indeholder data, der kunne fortolkes som XML-markup, men som ikke bør fortolkes som XML-markup.
De vigtigste forskelle mellem CDATA og kommentarer er:
]]>
(CDEnd
), mens det i en kommentar --
er ugyldigt.Det betyder, at givet disse tre XML-snippets fra et velformet dokument:
<!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>