XML CDATA的有关说明

许多地方都用到了CDATA部件,为此特地收集了一些相关资料说明。

CDATA为XmlNodeType的一个枚举值.
CDATA节。

示例 XML:<![CDATA[my escaped text]]>

CDATA 节用于转义否则将被识别为标记的文本块。CDATA 节点不能有任何子节点。它可以以 DocumentFragment、EntityReference 和 Element 节点子级的形式出现。

上面说得太简单了,以下来自http://www.w3schools.com/xml/xml_cdata.asp的说明:

All text in an XML document will be parsed by the parser.

Only text inside a CDATA section will be ignored by the parser.


Parsed Data

XML parsers normally parse all the text in an XML document.

When an XML element is parsed, the text between the XML tags is also parsed:

<message>This text is also parsed</message>

The parser does this because XML elements can contain other elements, as in this example, where the <name> element contains two other elements (first and last):

<name><first>Bill</first><last>Gates</last></name>

and the parser will break it up into sub-elements like this:

<name>
   <first>Bill</first>
   <last>Gates</last>
</name>


Escape Characters

Illegal XML characters have to be replaced by entity references.

If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element. You cannot write something like this:

<message>if salary < 1000 then</message>

To avoid this, you have to replace the "<" character with an entity reference, like this:

<message>if salary &lt; 1000 then</message>

There are 5 predefined entity references in XML:

&lt;<less than
&gt;>greater than
&amp;&ampersand 
&apos;'apostrophe
&quot;"quotation mark

Note: Only the characters "<" and "&" are strictly illegal in XML. Apostrophes, quotation marks and greater than signs are legal, but it is a good habit to replace them.


CDATA

Everything inside a CDATA section is ignored by the parser.

If your text contains a lot of "<" or "&" characters - as program code often does - the XML element can be defined as a CDATA section.

A CDATA section starts with "<![CDATA[" and ends with "]]>":

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
   {
   return 1
   }
else
   {
   return 0
   }
}
]]>
</script>

In the example above, everything inside the CDATA section is ignored by the parser.

Notes on CDATA sections:

A CDATA section cannot contain the string "]]>", therefore, nested CDATA sections are not allowed.

Also make sure there are no spaces or line breaks inside the "]]>" string

简单译文:

在XML文档中的所有文本都会被解析器解析。

只有在CDATA部件之内的文本会被解析器忽略。


解析数据

XML 解析器通常情况下会处理XML文档中的所有文本。

当XML元素被解析的时候,XML元素内部的文本也会被解析:

<message>This text is also parsed</message>

XML解析器这样做的原因是XML元素内部可能还包含了别的元素,象下面的例子,name元素内部包含了first和last两个元素:

<name><first>Bill</first><last>Gates</last></name>

解析器会认为上面的代码是这样的:

<name>
<first>Bill</first>
<last>Gates</last>
</name>


转义字符

不合法的XML字符必须被替换为相应的实体。

如果在XML文档中使用类似"<" 的字符, 那么解析器将会出现错误,因为解析器会认为这是一个新元素的开始。所以不应该象下面那样书写代码:

<message>if salary < 1000 then</message>

为了避免出现这种情况,必须将字符"<" 转换成实体,象下面这样:

<message>if salary &lt; 1000 then</message>

下面是五个在XML文档中预定义好的实体:

&lt;<小于号
&gt;>大于号
&amp;&
&apos;'单引号
&quot;"双引号

实体必须以符号"&"开头,以符号";"结尾

注意: 只有"<" 字符和"&"字符对于XML来说是严格禁止使用的。剩下的都是合法的,为了减少出错,使用实体是一个好习惯。


CDATA部件

在CDATA内部的所有内容都会被解析器忽略。

如果文本包含了很多的"<"字符和"&"字符——就象程序代码一样,那么最好把他们都放到CDATA部件中。

一个 CDATA 部件以"<![CDATA[" 标记开始,以"]]>"标记结束:

<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1
}
else
{
return 0
}
}
]]>
</script>

在前面的例子中,所有在CDATA部件之间的文本都会被解析器忽略。

CDATA注意事项:

CDATA部件之间不能再包含CDATA部件(不能嵌套)。如果CDATA部件包含了字符"]]>" 或者"<![CDATA[" ,将很有可能出错哦。

同样要注意在字符串"]]>"之间没有空格或者换行符。

简单例子:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/xsl">
<script>
<![CDATA[
function matchwo(a,b)
{
if (a < b && a < 0) then
{
return 1
}
else
{
return 0
}
}
]]>
</script>
<xsl:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<xsl:for-each select="CATALOG/CD">
<tr>
<td><xsl:value-of select="TITLE"/></td>
<td><xsl:value-of select="ARTIST"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值