我经常在文件中发现这个奇怪CDATA
的标签:XML
<![CDATA[some stuff]]>
我观察到这个CDATA
标签总是出现在开头,然后是一些东西。
但有时它被使用,有时它不是。我假设它是标记那some stuff
是之后将插入的“数据”。但什么样的数据是some stuff
?我在 XML 标记中写的任何东西都不是某种数据吗?
CDATA代表字符数据,这意味着这些字符串之间的数据包括可以解释为 XML 标记的数据,但不应如此。
CDATA 和注释之间的主要区别是:
这意味着给定来自一个格式良好的文档的这四个 XML 片段:
<!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 部分是“元素内容的一部分,被标记为解析器仅解释为字符数据,而不是标记。 ”
在语法上,它的行为类似于注释:
<exampleOfAComment>
<!--
Since this is a comment
I can use all sorts of reserved characters
like > < " and &
or write things like
<foo></bar>
but my document is still well-formed!
-->
</exampleOfAComment>
...但它仍然是文件的一部分:
<exampleOfACDATA>
<![CDATA[
Since this is a CDATA section
I can use all sorts of reserved characters
like > < " and &
or write things like
<foo></bar>
but my document is still well formed!
]]>
</exampleOfACDATA>
尝试将以下内容保存为.xhtml
文件(不是 .html
)并使用 FireFox(不是 Internet Explorer)打开它,以查看注释和 CDATA 部分之间的区别;当您在浏览器中查看文档时,注释不会出现,而 CDATA 部分将:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" >
<head>
<title>CDATA Example</title>
</head>
<body>
<h2>Using a Comment</h2>
<div id="commentExample">
<!--
You won't see this in the document
and can use reserved characters like
< > & "
-->
</div>
<h2>Using a CDATA Section</h2>
<div id="cdataExample">
<![CDATA[
You will see this in the document
and can use reserved characters like
< > & "
]]>
</div>
</body>
</html>
CDATA 部分需要注意的是它们没有编码,因此无法]]>
在其中包含字符串。据我所知,任何包含的字符数据]]>
都必须是文本节点。同样,从 DOM 操作的角度来看,您不能创建包含以下内容的 CDATA 部分]]>
:
var myEl = xmlDoc.getElementById("cdata-wrapper");
myEl.appendChild(xmlDoc.createCDATASection("This section cannot contain ]]>"));
此 DOM 操作代码将引发异常(在 Firefox 中)或导致结构不佳的 XML 文档:http: //jsfiddle.net/9NNHA/
一个大用例:您的 xml 包含一个程序,作为数据(例如 Java 的网页教程)。在这种情况下,您的数据包含大量字符,包括“&”和“<”,但这些字符并不意味着是 xml。
比较:
<example-code>
while (x < len && !done) {
print( "Still working, 'zzz'." );
++x;
}
</example-code>
和
<example-code><![CDATA[
while (x < len && !done) {
print( "Still working, 'zzzz'." );
++x;
}
]]></example-code>
特别是如果您从文件中复制/粘贴此代码(或将其包含在预处理器中),最好在 xml 文件中包含您想要的字符,而不会将它们与 XML 标记/属性混淆。正如@paary 提到的,其他常见用途包括嵌入包含与号的 URL 时。最后,即使数据只包含一些特殊字符但数据非常长(比如一章的文本),在编辑 xml 文件时不必对这几个实体进行编码/解码也很不错.
(我怀疑所有与评论的比较都具有误导性/无益。)
当我的 xml 元素需要存储 HTML 代码时,我曾经不得不使用 CDATA。就像是
<codearea>
<![CDATA[
<div> <p> my para </p> </div>
]]>
</codearea>
因此 CDATA 意味着它将忽略任何可能被解释为 XML 标记(如 < 和 > 等)的字符。
其中包含的数据不会被解析为 XML,因此不需要是有效的 XML,也可以包含看似 XML 但不是的元素。
作为其使用的另一个例子:
如果您有一个 RSS Feed(xml 文档)并希望在描述的显示中包含一些基本的 HTML 编码,您可以使用 CData 对其进行编码:
<item>
<title>Title of Feed Item</title>
<link>/mylink/article1</link>
<description>
<![CDATA[
<p>
<a href="/mylink/article1"><img style="float: left; margin-right: 5px;" height="80" src="/mylink/image" alt=""/></a>
Author Names
<br/><em>Date</em>
<br/>Paragraph of text describing the article to be displayed</p>
]]>
</description>
</item>
RSS 阅读器提取描述并在 CDATA 中呈现 HTML。
注意 - 并非所有 HTML 标记都有效 - 我认为这取决于您使用的 RSS 阅读器。
并解释为什么这个例子使用 CData(而不是适当的 pubData 和 dc:creator 标签):这是使用我们没有真正格式控制的 RSS 小部件的网站显示。
这使我们能够指定包含图像的高度和位置,正确格式化作者姓名和日期等等,而不需要新的小部件。这也意味着我可以编写脚本而不必手动添加它们。
来自维基百科:
[在] XML 文档或外部解析实体中,CDATA 部分是元素内容的一部分,被标记为解析器仅将其解释为字符数据,而不是标记。
因此:解析器可以看到 CDATA 中的文本,但只能作为字符而不是 XML 节点。
CDATA 代表字符数据。您可以使用它来转义一些否则将被视为常规 XML 的字符。这里面的数据不会被解析。例如,如果要传递包含其中的 URL,&
则可以使用 CDATA 来完成。否则,您将收到错误,因为它将被解析为常规 XML。
它转义了一个不能像往常一样传递给 XML 的字符串:
例子:
字符串中包含“&”。
你不能:
<FL val="Company Name">Dolce & Gabbana</FL>
因此,您必须使用 CDATA:
<FL val="Company Name"> <![CDATA["Dolce & Gabbana"]]> </FL>
它用于包含可能被视为 xml 的数据,因为它包含某些字符。
这样内部的数据将被显示,但不会被解释。
Cdata 是您可能希望传递给 xml 解析器但仍未解释为 xml 的数据。
比如说:-您有一个封装了问题/答案对象的 xml。此类开放字段可以包含不严格属于基本数据类型或 xml 定义的自定义数据类型的任何数据。喜欢 -这是 xml 注释的正确标记吗? .-- 您可能需要按原样传递它,而不会被 xml 解析器解释为另一个子元素。Cdata 来救你了。通过声明为 Cdata 您告诉解析器不要将包装为 xml 的数据处理(尽管它可能看起来像一个)
通常用于在 XML 文档中嵌入自定义数据,例如图片或声音数据。
请注意,CDATA
仅当将文本直接放在 XML 文本文件中时才需要该构造。
也就是说,您只需要使用CDATA
手动输入或直接以编程方式构建 XML 文本。
使用 DOM 处理器 API 或 SimpleXML 输入的任何文本都将自动转义,以防止违反 XML 内容规则。
尽管如此,有时 usingCDATA
可以减少文本大小,否则会产生所有实体编码,例如标签中的 cssstyle
或标签中的 javascript script
,其中许多语言构造使用 HTML|XML 中的字符,如<
和>
.