Xml 简明教程

XML - CDATA Sections

在本章中,我们将讨论 XML CDATA section 。术语 CDATA 表示,字符数据,CDATA 定义为解析器不解析的文本块,但仍作为标记识别。

In this chapter, we will discuss XML CDATA section. The term CDATA means, Character Data. CDATA is defined as blocks of text that are not parsed by the parser, but are otherwise recognized as markup.

&amplt;, &ampgt;,&ampamp; 这样的预定义实体需要输入,并且在标记中通常很难阅读,在这种情况下,可以使用 CDATA 部分,通过使用 CDATA 部分,您可以命令解析器,文档的特定部分不包含标记,并且应视为普通文本。

The predefined entities such as <, >, and & require typing and are generally difficult to read in the markup. In such cases, CDATA section can be used. By using CDATA section, you are commanding the parser that the particular section of the document contains no markup and should be treated as regular text.

Syntax

以下是 CDATA 部分的语法:

Following is the syntax for CDATA section −

<![CDATA[
   characters with markup
]]>

上面语法由三部分组成:

The above syntax is composed of three sections −

  1. CDATA Start section − CDATA begins with the nine-character delimiter <![CDATA[

  2. CDATA End section − CDATA section ends with ]]> delimiter.

  3. CData section − Characters between these two enclosures are interpreted as characters, and not as markup. This section may contain markup characters (<, >, and &), but they are ignored by the XML processor.

Example

以下标记代码显示了一个 CDATA 示例,在该示例中,解析器会忽略在 CDATA 部分内写的每个字符。

The following markup code shows an example of CDATA. Here, each character written inside the CDATA section is ignored by the parser.

<script>
   <![CDATA[
      <message> Welcome to TutorialsPoint </message>
   ]] >
</script >

在上面语法中,<message> 和 </message> 之间的所有内容都作为字符数据处理,而不是作为标记。

In the above syntax, everything between <message> and </message> is treated as character data and not as markup.

CDATA Rules

XML CDATA 必须遵守给定的规则:

The given rules are required to be followed for XML CDATA −

  1. CDATA cannot contain the string "]]>" anywhere in the XML document.

  2. Nesting is not allowed in CDATA section.