XMLファイルの中に、この奇妙なCDATA
タグをよく見つけます。
<![CDATA[some stuff]]>
このCDATA
タグはいつも最初に来て、その後に何かが続くことを観察しました。
しかし、このタグが使われることもあれば、使われないこともあります。これは、some stuff
がその後に挿入される"data"であることを示すためだと考えています。でも、some stuff
ってどんなデータなんだろう?XMLタグで書いたものは何かのデータではないのでしょうか?
CDATAはCharacter Dataの略で、これらの文字列の間にあるデータには、XMLマークアップとして解釈できる*可能性があるが、解釈すべきではないデータが含まれていることを意味しています。
CDATAとコメントの主な違いは以下の通りです。
]]>
([CDEnd
][4]) を含むことができませんが、コメントでは --
は無効 です。つまり、これらの3つのXMLのスニペットは、1つの整形されたドキュメントから得られたものです。
<!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>