gpt4 book ai didi

xml - 文档中的多个 XML 声明是否为格式正确的 XML?

转载 作者:数据小太阳 更新时间:2023-10-29 01:54:51 30 4
gpt4 key购买 nike

在同一个文档中有两个 XML 声明是否是格式正确的 XML?

<?xml version="1.0" encoding="UTF-8"?>
<a>
<?xml version="1.0" encoding="UTF-8"?>
<b>
hello
</b>
</a>

我相信不是,但是我找不到支持我的消息来源。

来自 Extensible Markup Language (XML) 1.0

Definition: XML documents SHOULD begin with an XML declaration which specifies the version of XML being used.

讨厌的词“应该”就在那里。它说理想情况下文档以 XML 声明开始。它没有提到在文档中有另一个。

The document type declaration MUST appear before the first element in the document.

这很接近,但它没有讨论 XML 声明本身,即使它应该在它之前。

最佳答案

在格式良好的 XML 中只允许一个 XML 声明,并且它必须位于顶部(如果有的话)。

必须在顶部

参见Well-Formed XML Documents文档的定义节XML Recommendation :

[1]     document ::= prolog element Misc*

然后检查序言:

[22]    prolog   ::= XMLDecl? Misc* (doctypedecl Misc*)?

然后是XMLDecl:

[23]    XMLDecl  ::= '<?xml' VersionInfo EncodingDecl? SDDecl? S? '?>'

因此,我们看到 EBNF允许在文档顶部进行 XML 声明。

只有一个

Processing instructions ...

[16]    PI       ::= '<?' PITarget (S (Char* - (Char* '?>' Char*)))? '?>'
[17] PITarget ::= Name - (('X' | 'x') ('M' | 'm') ('L' | 'l'))

...通常可能出现在其他地方,但是根据 PITarget 的定义和此语句排除了第二个 XML 声明:

The target names " XML ", " xml ", and so on are reserved for standardization in this or future versions of this specification.

关于xml - 文档中的多个 XML 声明是否为格式正确的 XML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20251560/

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