XML 中的 <![CDATA[]]> 是什么意思?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2784183/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What does <![CDATA[]]> in XML mean?
提问by dontWatchMyProfile
I often find this strange CDATAtag in XMLfiles:
我经常CDATA在XML文件中发现这个奇怪的标签:
<![CDATA[some stuff]]>
I have observed that this CDATAtag always comes at the beginning, and then followed by some stuff.
我观察到这个CDATA标签总是出现在开头,然后是一些东西。
But sometimes it is used, sometimes it is not. I assume it is to mark that some stuffis 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?
但有时使用,有时不使用。我假设它是标记some stuff之后将插入的“数据”。但是什么样的数据some stuff呢?我在 XML 标签中写的任何东西都不是某种数据吗?
采纳答案by Sean Vieira
CDATAstands for Character Dataand it means that the data in between these strings includes data that couldbe interpreted as XML markup, but should not be.
CDATA代表字符数据,这意味着,在这些字符串之间的数据包括数据可能被解释为XML标记,但不应该是。
The key differences between CDATA and comments are:
CDATA 和注释之间的主要区别是:
- 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 Entityreferences are not recognized inside of comments.
- 正如Richard 指出的那样,CDATA 仍然是文档的一部分,而注释则不是。
- 在 CDATA 中,您不能包含字符串
]]>(CDEnd),而在注释中--是 invalid。 - 注释中无法识别参数实体引用。
This means given these four snippets of XML from one well-formed document:
这意味着从一个格式良好的文档中给出这四个 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>
回答by Richard JP Le Guen
A CDATA section is "a section of element content that is marked for the parser to interpret as only character data, not markup."
CDATA 部分是“被标记为供解析器仅解释为字符数据而非标记的元素内容部分。”
Syntactically, it behaves similarly to a comment:
在语法上,它的行为类似于注释:
<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>
... but it is still part of the document:
...但它仍然是文档的一部分:
<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>
Try saving the following as a .xhtmlfile (not.html) and open it using FireFox (not Internet Explorer) to see the difference between the comment and the CDATA section; the comment won't appear when you look at the document in a browser, while the CDATA section will:
尝试将以下内容另存为.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>
Something to take note of with CDATA sections is that they have no encoding, so there's no way to include the string ]]>in them. Any character data which contains ]]>will have to - as far as I know - be a text node instead. Likewise, from a DOM manipulation perspective you can't create a CDATA section which includes ]]>:
CDATA 部分需要注意的是它们没有编码,因此无法]]>在其中包含字符串。任何包含的字符数据]]>都必须 - 据我所知 - 改为文本节点。同样,从 DOM 操作的角度来看,您无法创建包含]]>以下内容的 CDATA 部分:
var myEl = xmlDoc.getElementById("cdata-wrapper");
myEl.appendChild(xmlDoc.createCDATASection("This section cannot contain ]]>"));
This DOM manipulation code will either throw an exception (in Firefox) or result in a poorly structured XML document: http://jsfiddle.net/9NNHA/
此 DOM 操作代码将引发异常(在 Firefox 中)或导致结构不良的 XML 文档:http: //jsfiddle.net/9NNHA/
回答by not-just-yeti
One big use-case: your xml includes a program, as data (e.g. a web-page tutorial for Java). In that situation your data includes a big chunk of characters that include '&' and '<' but those characters aren't meant to be xml.
一个大用例:您的 xml 包含一个程序,作为数据(例如 Java 的网页教程)。在这种情况下,您的数据包含大量字符,其中包括“&”和“<”,但这些字符并不意味着是 xml。
Compare:
相比:
<example-code>
while (x < len && !done) {
print( "Still working, 'zzz'." );
++x;
}
</example-code>
with
和
<example-code><![CDATA[
while (x < len && !done) {
print( "Still working, 'zzzz'." );
++x;
}
]]></example-code>
Especially if you are copy/pasting this code from a file (or including it, in a pre-processor), it's nice to just have the characters you want in your xml file, w/o confusing them with XML tags/attributes. As @paary mentioned, other common uses include when you're embedding URLs that contain ampersands. Finally, even if the data only contains a few special characters but the data is very very long (the text of a chapter, say), it's nice to not have to be en/de-coding those few entities as you edit your xml file.
特别是如果您从文件中复制/粘贴此代码(或将其包含在预处理器中),那么在 xml 文件中只包含您想要的字符就好了,而不会将它们与 XML 标记/属性混淆。正如@paary 所提到的,其他常见用途包括嵌入包含与符号的 URL 时。最后,即使数据只包含几个特殊字符但数据非常长(比如一章的文本),在编辑 xml 文件时不必对这几个实体进行编码/解码也很好.
(I suspect all the comparisons to comments are kinda misleading/unhelpful.)
(我怀疑所有与评论的比较都有些误导/无益。)
回答by Octane
I once had to use CDATA when my xml element needed to store HTML code. Something like
当我的 xml 元素需要存储 HTML 代码时,我曾经不得不使用 CDATA。就像是
<codearea>
<![CDATA[
<div> <p> my para </p> </div>
]]>
</codearea>
So CDATA means it will ignore any character which could otherwise be interpreted as XML tag like < and > etc.
所以 CDATA 意味着它将忽略任何可能被解释为 XML 标记的字符,如 < 和 > 等。
回答by fbrereto
The data contained therein will not be parsed as XML, and as such does not need to be valid XML or can contain elements that may appear to be XML but are not.
其中包含的数据不会被解析为 XML,因此不需要是有效的 XML 或可以包含可能看起来是 XML 但实际上不是 XML 的元素。
回答by Chdid
From Wikipedia:
来自维基百科:
[In] an XML document or external parsed entity, a CDATA section is a section of element content that is marked for the parser to interpret as only character data, not markup.
[在] XML 文档或外部解析实体中,CDATA 部分是被标记为解析器仅解释为字符数据而非标记的元素内容部分。
Thus: text inside CDATA is seen by the parser but only as characters not as XML nodes.
因此:解析器可以看到 CDATA 中的文本,但只能作为字符而不是 XML 节点。
回答by LadyCygnus
As another example of its use:
作为其使用的另一个示例:
If you have an RSS Feed (xml document) and want to include some basic HTML encoding in the display of the description, you can use CData to encode it:
如果您有一个 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>
The RSS Reader pulls in the description and renders the HTML within the CDATA.
RSS 阅读器提取描述并在 CDATA 中呈现 HTML。
Note - not all HTML tags work - I think it depends on the RSS reader you are using.
注意 - 并非所有 HTML 标签都有效 - 我认为这取决于您使用的 RSS 阅读器。
And as a explanation for why this example uses CData (and not the appropriate pubData and dc:creator tags): this is for website display using a RSS widget for which we have no real formatting control.
并解释为什么这个例子使用 CData(而不是适当的 pubData 和 dc:creator 标签):这是用于使用 RSS 小部件的网站显示,我们没有真正的格式控制。
This enables us to specify the height and position of the included image, format the author names and date correctly, and so forth, without the need for a new widget. It also means I can script this and not have to add them by hand.
这使我们能够指定包含图像的高度和位置,正确格式化作者姓名和日期等,而无需新的小部件。这也意味着我可以编写脚本,而不必手动添加它们。
回答by paary
CDATA stands for Character Data. You can use this to escape some characters which otherwise will be treated as regular XML. The data inside this will not be parsed.
For example, if you want to pass a URL that contains &in it, you can use CDATA to do it. Otherwise, you will get an error as it will be parsed as regular XML.
CDATA 代表字符数据。您可以使用它来转义某些字符,否则这些字符将被视为常规 XML。这里面的数据不会被解析。例如,如果要传递其中包含的 URL,&则可以使用 CDATA 来完成。否则,您将收到错误,因为它将被解析为常规 XML。
回答by Ikke
It's used to contain data which could otherwise be seen as xml because it contains certain characters.
它用于包含可能被视为 xml 的数据,因为它包含某些字符。
This way the data inside will be displayed, but not interpreted.
这样里面的数据将被显示,但不会被解释。
回答by HoangYell
It escapes a string that cannot be passed to XML as usual:
它转义了一个不能像往常一样传递给 XML 的字符串:
Example:
例子:
The string contains "&" in it.
字符串中包含“&”。
You can not:
你不能:
<FL val="Company Name">Dolce & Gabbana</FL>
Therefore, you must use CDATA:
因此,您必须使用 CDATA:
<FL val="Company Name"> <![CDATA["Dolce & Gabbana"]]> </FL>

