gpt4 book ai didi

xml - ]]> 序列在 XML 标记的内容中真的不合法吗?

转载 作者:数据小太阳 更新时间:2023-10-29 02:31:02 25 4
gpt4 key购买 nike

我有一个带有标签的 XML 文档,其中包含用户输入的消息,我想避免不必要的字符转义。

根据下面的链接,唯一严格非法的字符是“<”和“&”。

Note: Only the characters "<" and "&" are strictly illegal in XML.The greater than character is legal, but it is a good habit to replace it.

http://www.w3schools.com/xml/xml_syntax.asp

但在某些解析器中我遇到了序列问题 ]]>,这是由于解析器的问题还是它真的在 XML 标准的某处被定义为非法?

示例消息:

<?xml version="1.0" encoding="UTF-8" ?> 
<root>
<message>&lt;!-- -- -- &lt;![CDATA[&quot;TEST&quot;]]></message>
<signature>Evil</signature>
</root>

如您所见,< 和 & 被转义,此消息已被 C++ tinyxml 和 Java JAXB 成功解析。 Firefox 20.0.1 和 IE 8.0 都告诉我

XML Parsing Error: not well-formed

The literal string ']]>' is not allowed in element content.

分别。

这真的是标准的强制行为吗?

编辑: 看起来应该搜索更多,Legally use CDATA in XML .所以我猜 Firefox 和 IE 中的 XML 解析器刚刚坏了?

最佳答案

来自XML spec (强调我的):

The ampersand character (&) and the left angle bracket (<) MUST NOT appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they MUST be escaped using either numeric character references or the strings "&amp;" and "&lt;" respectively. The right angle bracket (>) may be represented using the string "&gt;", and MUST, for compatibility, be escaped using either "&gt;" or a character reference when it appears in the string "]]>" in content, when that string is not marking the end of a CDATA section.

这意味着只要 ]]> 定界符未被用于标记 CDATA 部分的结尾供读取此文档的 XML 解析器使用,它没有被转义是不合法的,即使它没有出现在 CDATA 部分的上下文中也是如此。

我不熟悉浏览器内部使用的 XML 解析器,但考虑到出于兼容性原因存在此要求,您的猜测似乎是正确的。

关于xml - ]]> 序列在 XML 标记的内容中真的不合法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16101197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com