我经常CDATA
在XML
文件中找到这个奇怪的标签:
<![CDATA[some stuff]]>
Run Code Online (Sandbox Code Playgroud)
我观察到这个CDATA
标签总是在开头,然后是一些东西.
但有时它被使用,有时则不然.我假设它是标记some stuff
那将在之后插入的"数据".但是什么样的数据some stuff
呢?我在XML标签中写的东西不是某种数据吗?
Sea*_*ira 915
CDATA代表字符数据,这意味着,在这些字符串之间的数据包括数据可能被解释为XML标记,但不应该是.
CDATA和评论之间的主要区别是:
这意味着从一个格式良好的文档中给出这三个XML片段:
<!ENTITY MyParamEntity "Has been expanded">
Run Code Online (Sandbox Code Playgroud)
<!--
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.
-->
Run Code Online (Sandbox Code Playgroud)
<![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.
]]>
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)
Ric*_*uen 331
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>
Run Code Online (Sandbox Code Playgroud)
......但它仍然是文件的一部分:
<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>
Run Code Online (Sandbox Code Playgroud)
尝试将以下内容保存为.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>
Run Code Online (Sandbox Code Playgroud)
CDATA部分需要注意的是它们没有编码,因此无法]]>
在其中包含字符串.]]>
根据我所知,包含的任何字符数据都必须是文本节点.同样,从DOM操作角度来看,您无法创建包含]]>
以下内容的CDATA部分:
var myEl = xmlDoc.getElementById("cdata-wrapper");
myEl.appendChild(xmlDoc.createCDATASection("This section cannot contain ]]>"));
Run Code Online (Sandbox Code Playgroud)
这个DOM操作代码将抛出异常(在Firefox中)或导致结构不良的XML文档:http://jsfiddle.net/9NNHA/
not*_*eti 67
一个很大的用例:你的xml包含一个程序,作为数据(例如Java的网页教程).在这种情况下,您的数据包含大量字符,包括"&"和"<",但这些字符不是xml.
相比:
<example-code>
while (x < len && !done) {
print( "Still working, 'zzz'." );
++x;
}
</example-code>
Run Code Online (Sandbox Code Playgroud)
同
<example-code><![CDATA[
while (x < len && !done) {
print( "Still working, 'zzzz'." );
++x;
}
]]></example-code>
Run Code Online (Sandbox Code Playgroud)
特别是如果您从文件中复制/粘贴此代码(或包含它,在预处理器中),那么只需在xml文件中包含所需的字符即可,而不会将它们与XML标记/属性混淆.正如@paary所提到的,其他常见用途包括嵌入包含&符号的URL.最后,即使数据只包含一些特殊字符,但数据非常长(例如章节的文本),在编辑xml文件时不必对这几个实体进行编码/编码很好.
(我怀疑所有与评论的比较都有点误导/无益.)
Oct*_*ane 38
当我的xml元素需要存储HTML代码时,我曾经不得不使用CDATA.就像是
<codearea>
<![CDATA[
<div> <p> my para </p> </div>
]]>
</codearea>
Run Code Online (Sandbox Code Playgroud)
所以CDATA意味着它将忽略任何可能被解释为XML标签的字符,如<和>等.
小智 12
来自维基百科:
[在] XML文档或外部解析实体中,CDATA部分是元素内容的一部分,标记为解析器仅解释为字符数据,而不是标记.
因此:解析器可以看到CDATA中的文本,但只作为字符而不是XML节点.
Lad*_*nus 11
作为其使用的另一个例子:
如果您有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>
Run Code Online (Sandbox Code Playgroud)
RSS阅读器提取描述并在CDATA中呈现HTML.
注意 - 并非所有HTML标记都有效 - 我认为这取决于您使用的RSS阅读器.
并解释为什么这个例子使用CData(而不是相应的pubData和dc:creator标签):这是用于使用RSS小部件进行网站显示,我们没有真正的格式控制.
这使我们能够指定所包含图像的高度和位置,正确格式化作者姓名和日期,等等,而无需新的小部件.这也意味着我可以编写脚本,而不必手动添加它们.
它转义了一个不能像往常一样传递给 XML 的字符串:
例子:
字符串中包含“&”。
你不能:
<FL val="Company Name">Dolce & Gabbana</FL>
Run Code Online (Sandbox Code Playgroud)
因此,您必须使用 CDATA:
<FL val="Company Name"> <![CDATA["Dolce & Gabbana"]]> </FL>
Run Code Online (Sandbox Code Playgroud)
小智 8
CDATA代表字符数据.您可以使用它来转义某些字符,否则这些字符将被视为常规XML.其中的数据将不会被解析.例如,如果要传递包含&
在其中的URL,则可以使用CDATA执行此操作.否则,您将收到错误,因为它将被解析为常规XML.