有没有办法在 xml 中转义 CDATA 结束标记? [英] Is there a way to escape a CDATA end token in xml?

查看:29
本文介绍了有没有办法在 xml 中转义 CDATA 结束标记?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有任何方法可以在 xml 文档的 CDATA 部分中转义 CDATA 结束标记 (]]>).或者,更一般地说,如果在 CDATA 中使用了一些转义序列(但如果它存在,我想它可能只对转义开始或结束标记才有意义,无论如何).

I was wondering if there is any way to escape a CDATA end token (]]>) within a CDATA section in an xml document. Or, more generally, if there is some escape sequence for using within a CDATA (but if it exists, I guess it'd probably only make sense to escape begin or end tokens, anyway).

基本上,您能否在 CDATA 中嵌入开始或结束标记,并告诉解析器不要解释它,而是将其视为另一个字符序列.

Basically, can you have a begin or end token embedded in a CDATA and tell the parser not to interpret it but to treat it as just another character sequence.

也许,如果您发现自己尝试这样做,您应该重构您的 xml 结构或您的代码,但即使我在过去 3 年左右的时间里每天都在使用 xml 并且我从来没有这个问题,我想知道是否有可能.只是出于好奇.

Probably, you should just refactor your xml structure or your code if you find yourself trying to do that, but even though I've been working with xml on a daily basis for the last 3 years or so and I have never had this problem, I was wondering if it was possible. Just out of curiosity.

除了使用 html 编码...

Other than using html encoding...

推荐答案

显然,这个问题纯粹是学术性的.幸运的是,它有一个非常明确的答案.

Clearly, this question is purely academic. Fortunately, it has a very definite answer.

您不能转义 CDATA 结束序列.XML 规范的生产规则 20 非常清楚:

You cannot escape a CDATA end sequence. Production rule 20 of the XML specification is quite clear:

[20]    CData      ::=      (Char* - (Char* ']]>' Char*))

此产品规则字面意思是CData 部分可能包含您想要的任何内容,但序列 ']]>".也不例外.".

This product rule literally means "A CData section may contain anything you want BUT the sequence ']]>'. No exception.".

同一部分还写道:

在 CDATA 部分中,只有 CDEnd 字符串被识别为标记,因此左尖括号和 & 符号可能以其文字形式出现;它们不需要(也不能)使用<"进行转义.和&".CDATA 节不能嵌套.

Within a CDATA section, only the CDEnd string is recognized as markup, so that left angle brackets and ampersands may occur in their literal form; they need not (and cannot) be escaped using "<" and "&". CDATA sections cannot nest.

换句话说,不可能使用实体引用、标记或任何其他形式的解释语法.CDATA 部分中唯一解析的文本是 ]]>,它终止了该部分.

In other words, it's not possible to use entity reference, markup or any other form of interpreted syntax. The only parsed text inside a CDATA section is ]]>, and it terminates the section.

因此,不可能在 CDATA 部分中转义 ]]>.

Hence, it is not possible to escape ]]> within a CDATA section.

同一部分还写道:

2.7 CDATA 部分

2.7 CDATA Sections

[定义:CDATA 节可能出现在任何可能出现字符数据的地方;它们用于转义包含字符的文本块,否则这些字符将被识别为标记.CDATA 部分以字符串<![CDATA[";并以字符串]]>"结尾:]

[Definition: CDATA sections may occur anywhere character data may occur; they are used to escape blocks of text containing characters which would otherwise be recognized as markup. CDATA sections begin with the string "<![CDATA[" and end with the string "]]>":]

然后在任何可能出现字符数据的地方都有一个 CDATA 部分,包括多个相邻的 CDATA 部分而不是单个 CDATA 部分.这使得可以拆分 ]]> 标记并将它的两部分放在相邻的 CDATA 部分中.

Then there may be a CDATA section anywhere character data may occur, including multiple adjacent CDATA sections inplace of a single CDATA section. That allows it to be possible to split the ]]> token and put the two parts of it in adjacent CDATA sections.

例如:

<![CDATA[Certain tokens like ]]> can be difficult and <invalid>]]> 

应该写成

<![CDATA[Certain tokens like ]]]]><![CDATA[> can be difficult and <valid>]]> 

这篇关于有没有办法在 xml 中转义 CDATA 结束标记?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆