什么是 <![CDATA[]]>在 XML 中是什么意思? [英] What does <![CDATA[]]> in XML mean?
问题描述
我经常在 XML
文件中发现这个奇怪的 CDATA
标签:
I often find this strange CDATA
tag in XML
files:
<![CDATA[some stuff]]>
我观察到这个 CDATA
标签总是出现在开头,然后是一些东西.
I have observed that this CDATA
tag always comes at the beginning, and then followed by some stuff.
但有时使用,有时不使用.我假设它是标记 some stuff
是将在此之后插入的数据".但是一些东西
是什么类型的数据?我在 XML 标签中写的任何东西都不是某种数据吗?
But sometimes it is used, sometimes it is not. I assume it is to mark that some stuff
is the "data" that will be inserted after that. But what kind of data is some stuff
? Isn't anything I write in XML tags some sort of data?
推荐答案
CDATA 代表 字符数据,这意味着这些字符串之间的数据包括可以被解释为 XML 标记但不应该被解释的数据.
CDATA stands for Character Data and it means that the data in between these strings includes data that could be interpreted as XML markup, but should not be.
CDATA 和注释之间的主要区别是:
The key differences between CDATA and comments are:
- 正如 Richard 指出的,CDATA 仍然是文档的一部分,而注释则不是.
- 在 CDATA 中,您不能包含字符串
]]>
(CDEnd
),而在评论中--
无效. - 参数实体在评论中无法识别.
- As Richard points out, CDATA is still part of the document, while a comment is not.
- In CDATA you cannot include the string
]]>
(CDEnd
), while in a comment--
is invalid. - Parameter Entity references are not recognized inside of comments.
这意味着从一个格式良好的文档中给出这四个 XML 片段:
This means given these four snippets of XML from one well-formed document:
<!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>
这篇关于什么是 <![CDATA[]]>在 XML 中是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!