XML中的<!]>是什么意思?

我经常在XML文件中find这个奇怪的CDATA标签:

 <![CDATA[some stuff]]> 

我注意到,这个CDATA标签总是在开头,然后是一些东西。

但是有时它被使用,有时不是。 我认为这是为了标记some stuff是在那之后插入的“数据”。 但是什么样的数据是some stuff ? 是不是我用XML标记某种数据的东西?

CDATA代表字符数据 ,这意味着这些string之间的数据包含可以被解释为XML标记的数据,但不应该是。

CDATA和评论之间的主要区别是:

  • 理查德指出 ,CDATA仍然是文件的一部分,而评论则不是。
  • 在CDATA中,不能包含string]]>CDEnd ),而在注释中无效 。
  • 参数实体引用在注释内部不被识别。

这意味着从一个格式良好的文档中给出这三个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部分是“ 标记为parsing器仅解释为字符数据而不是标记的元素内容的一部分 ”。

在语法上,它的行为与评论类似:

 <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部分没有编码,所以没有办法在其中包含string]]> 。 任何包含]]>字符数据都必须 – 据我所知 – 是一个文本节点。 同样,从DOM操作的angular度来看,你不能创build一个包含]]>的CDATA部分:

 var myEl = xmlDoc.getElementById("cdata-wrapper"); myEl.appendChild(xmlDoc.createCDATASection("This section cannot contain ]]>")); 

这个DOM操作代码将会抛出一个exception(在Firefox中),或者导致结构不好的XML文档: http : //jsfiddle.net/9NNHA/

一个大的用例:你的xml包含一个程序,作为数据(例如Java的网页教程)。 在这种情况下,你的数据包含一大块包含“&”和“<”的字符,但是这些字符并不是xml。

比较:

 <example-code> while (x &lt; len &amp;&amp; !done) { print( &quot;Still working, &apos;zzz&apos;.&quot; ); ++x; } </example-code> 

 <example-code><![CDATA[ while (x < len && !done) { print( "Still working, 'zzzz'." ); ++x; } ]]></example-code> 

特别是如果你从一个文件(或者包含它,在一个预处理器中)拷贝/粘贴这个代码,只需要在你的xml文件中有你想要的字符,而不用把它们与XML标签/属性混淆。 正如@paary提到的,其他常见用途包括embedded包含&符号的URL。 最后,即使数据只包含一些特殊字符,但是数据非常长(例如,章节的内容),在编辑XML文件时,不必对这些less数实体进行en / de-coding 。

(我怀疑所有的评论比较有点误导/无益)。

当我的XML标签需要存储HTML代码时,我曾经使用过CDATA。 就像是

 <codearea> <![CDATA[ <div> <p> my para </p> </div> ]]> </codearea> 

所以CDATA意味着它会忽略任何可能被解释为XML标签的字符,如<和>等。

其中包含的数据将不会被parsing为XML,因此不需要是有效的XML或可以包含可能看起来是XML但不是。

来自http://en.wikipedia.org/wiki/CDATA :“一个XML文档或外部parsing的实体,一个CDATA部分是元素内容的一部分,标记为parsing器只解释为字符数据,而不是标记。

因此CDATA中的文本可以被parsing器看到,但是只能看作不是XML节点的字符。

CDATA代表字符数据。 你可以使用它来转义一些字符,否则这些字符将被视为普通的XML。 里面的数据不会被parsing。 例如,如果你想传递一个包含&的URL,你可以使用CDATA来做到这一点。 否则,你会得到一个错误,因为它将被parsing为普通的XML。

作为另一个使用的例子…

如果您有一个RSS源(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小部件,我们没有真正的格式控制。

这使我们能够指定包含的图像的高度和位置,正确地格式化作者姓名和date等,而不需要新的小部件。 这也意味着我可以编写脚本,而不必手动添加它们。

它被用来包含数据,否则可能被视为XML,因为它包含某些字符。

这样,里面的数据将被显示,但不会被解释。

通常用于embedded自定义数据,如XML文档中的图片或声音数据。

Cdata是一个数据,你可能想传递给一个XMLparsing器,仍然不解释为一个XML。

说例如: – 你有一个封装问题/答案对象的XML。 这样的开放字段可以有任何不严格属于基本数据types或xml定义的自定义数据types的数据。 喜欢 – 这是一个正确的标签为XML评论? .–您可能需要按原样传递它,而不被xmlparsing器解释为另一个子元素。 这里Cdata来救援。 通过声明为Cdata你告诉parsing器不要把数据包装成一个xml(虽然它看起来像一个)