是什么意思?在XML中是什么意思?

发布于 2024-09-01 07:55:40 字数 299 浏览 3 评论 0原文

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

<![CDATA[some stuff]]>

我观察到这个 CDATA 标签总是出现在开头,然后是一些东西。

但有时会用到,有时则不会。我认为它是为了标记 some stuff 是之后将插入的“数据”。但是some stuff 是什么样的数据呢?我在 XML 标签中编写的任何内容不都是某种数据吗?

I often find this strange CDATA tag in XML files:

<![CDATA[some stuff]]>

I have observed that this CDATA tag always comes at the beginning, and then followed by some stuff.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(13

不顾 2024-09-08 07:55:40

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 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.

The key differences between CDATA and comments are:

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>
云裳 2024-09-08 07:55:40

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/

A CDATA section is "a section of element content that is marked for the parser to interpret as only character data, not markup."

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 .xhtml file (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:

<?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 ]]>:

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/

难忘№最初的完美 2024-09-08 07:55:40

一个重要的用例:您的 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 文件时不必对这几个实体进行编码/解码是件好事。

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

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.

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.

(I suspect all the comparisons to comments are kinda misleading/unhelpful.)

公布 2024-09-08 07:55:40

当我的 xml 元素需要存储 HTML 代码时,我曾经不得不使用 CDATA。像

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

这样的 CDATA 意味着它将忽略任何可能被解释为 XML 标记的字符,例如 <和> ETC。

I once had to use CDATA when my xml element needed to store HTML code. Something like

<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.

揽月 2024-09-08 07:55:40

其中包含的数据不会被解析为 XML,因此不需要是有效的 XML,也可以包含看似 XML 但实际上并非如此的元素。

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.

陌路终见情 2024-09-08 07:55:40

作为其使用的另一个示例:

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

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

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:

<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.

Note - not all HTML tags work - I think it depends on the RSS reader you are using.


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.

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.

肩上的翅膀 2024-09-08 07:55:40

来自维基百科:

[在] XML 文档或外部解析实体中,CDATA 部分是
标记为供解析器解释的元素内容部分
仅作为字符数据,而不是标记。

http://en.wikipedia.org/wiki/CDATA

因此:CDATA 中的文本可以由解析器,但仅作为字符而不是 XML 节点。

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.

http://en.wikipedia.org/wiki/CDATA

Thus: text inside CDATA is seen by the parser but only as characters not as XML nodes.

旧伤慢歌 2024-09-08 07:55:40

它对无法像往常一样传递给 XML 的字符串进行转义:

示例:

字符串包含“&”

您不能:

<FL val="Company Name">Dolce & Gabbana</FL>

因此,您必须使用 CDATA:

<FL val="Company Name"> <![CDATA["Dolce & Gabbana"]]> </FL>

It escapes a string that cannot be passed to XML as usual:

Example:

The string contains "&" in it.

You can not:

<FL val="Company Name">Dolce & Gabbana</FL>

Therefore, you must use CDATA:

<FL val="Company Name"> <![CDATA["Dolce & Gabbana"]]> </FL>
等待圉鍢 2024-09-08 07:55:40

CDATA 代表字符数据。您可以使用它来转义某些字符,否则这些字符将被视为常规 XML。其中的数据不会被解析。
例如,如果您想传递一个包含 & 的 URL,您可以使用 CDATA 来完成。否则,您将收到错误,因为它将被解析为常规 XML。

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.

明媚殇 2024-09-08 07:55:40

它用于包含可能被视为 xml 的数据,因为它包含某些字符。

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

It's used to contain data which could otherwise be seen as xml because it contains certain characters.

This way the data inside will be displayed, but not interpreted.

一世旳自豪 2024-09-08 07:55:40

Cdata 是您可能想要传递给 xml 解析器但仍未解释为 xml 的数据。

例如:- 您有一个 xml,其中封装了问题/答案对象。此类开放字段可以包含严格不属于基本数据类型或 xml 定义的自定义数据类型的任何数据。就像 --这是 xml 注释的正确标签吗? .--
您可能需要按原样传递它,而不被 xml 解析器解释为另一个子元素。 Cdata 来拯救你了。通过声明为 Cdata,您告诉解析器不要将包装为 xml 的数据处理(尽管它可能看起来像一个 )

The Cdata is a data which you may want to pass to an xml parser and still not interpreted as an xml.

Say for eg :- You have an xml which has encapsulates question/answer object . Such open fields can have any data which does not strictly fall under basic data type or xml defined custom data types. Like --Is this a correct tag for xml comment ? .--
You may have a requirement to pass it as it is without being interpreted by the xml parser as another child element. Here Cdata comes to your rescue . By declaring as Cdata you are telling the parser don't treat the data wrapped as an xml (though it may look like one )

韶华倾负 2024-09-08 07:55:40

请注意,仅当将文本直接放入 XML 文本文件时才需要 CDATA 构造。

也就是说,如果直接手动输入或以编程方式构建 XML 文本,则只需使用 CDATA

使用 DOM 处理器 API 或 SimpleXML 输入的任何文本都将自动转义,以防止违反 XML 内容规则。

尽管如此,有时使用 CDATA 可以减小文本大小,否则会使用所有编码的实体生成文本大小,例如 style 标签中的 css 或 style 标签中的 javascript code>script 标签,其中许多语言结构使用 HTML|XML 中的字符,例如 <>

Note that the CDATA construct is only needed if placing text directly in the XML text file.

That is, you only need to use CDATA if hand typing or programmatically building the XML text directly.

Any text entered using a DOM processor API or SimpleXML will be automatically escaped to prevent running foul of XML content rules.

Notwithstanding that, there can be times where using CDATA can reduce the text size that would otherwise be produced with all entities encoded, such as for css in style tags or javascript in script tags, where many language constructs use characters in HTML|XML, like < and >.

提笔落墨 2024-09-08 07:55:40

通常用于在 XML 文档中嵌入自定义数据,例如图片或声音数据。

Usually used for embedding custom data, like pictures or sound data within an XML document.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文