Ich finde oft dieses seltsame CDATA
-Tag in XML
-Dateien:
<![CDATA[some stuff]]>
Ich habe beobachtet, dass dieser CDATA
-Tag immer am Anfang steht und dann von einigen Dingen gefolgt wird.
Aber manchmal wird es verwendet, manchmal nicht. Ich nehme an, es soll kennzeichnen, dass "etwas" die "Daten" sind, die danach eingefügt werden. Aber welche Art von Daten ist einiges Zeug
? Ist nicht alles, was ich in XML-Tags schreibe, eine Art von Daten?
CDATA steht für Character Data und bedeutet, dass die Daten zwischen diesen Strings Daten enthalten, die als XML-Markup interpretiert werden könnten, aber nicht sollten.
Die wichtigsten Unterschiede zwischen CDATA und Kommentaren sind:
]]>
(CDEnd
) nicht einschließen, während in einem Kommentar --
ungültig ist.Dies bedeutet, dass diese drei XML-Schnipsel aus einem wohlgeformten Dokument gegeben sind:
<!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>
Es wird verwendet, um Daten zu enthalten, die sonst als xml angesehen werden könnten, weil sie bestimmte Zeichen enthalten.
Auf diese Weise werden die darin enthaltenen Daten angezeigt, aber nicht interpretiert.