文档中的多个 XML 声明是否是格式良好的 XML? [英] Are multiple XML declarations in a document well-formed XML?

查看:36
本文介绍了文档中的多个 XML 声明是否是格式良好的 XML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is having two XML declarations in the same document well-formed XML?

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

我相信它不是,但是我找不到支持我的来​​源.

I believe it is not, however I can't find a source to back me up on this.

来自 可扩展标记语言 (XML) 1.0

定义:XML 文档应该以 XML 声明开始指定正在使用的 XML 版本.

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

讨厌的词应该"就在那里.它说理想情况下文档以 XML 声明开头.它没有说明在文档中包含另一个.

The pesky word "should" is there. It says ideally the document starts with an XML declaration. It says nothing about having another one within the document.

文档类型声明必须出现在第一个元素之前文件.

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

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

This is close, but it doesn't talk about the XML declaration itself, even though it should come before it.

推荐答案

在格式良好的 XML 中只允许一个 XML 声明,并且它必须在顶部 如果在任何地方.

Only one XML declaration is permitted in well-formed XML, and it must be at the top if anywhere.

查看Well-Formed中document的定义XML 建议的 XML 文档部分:

See the definition of document in the Well-Formed XML Documents section of the XML Recommendation:

[1]     document ::= prolog element Misc*

然后检查prolog:

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

然后XMLDecl:

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

因此,我们看到 EBNF 允许在文档顶部.

So, we see that the EBNF permits an XML declaration at the top of the document.

处理说明...

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

...通常可能会出现在其他地方,但由于 PITarget 的定义和以下声明,排除了第二个 XML 声明:

...in general may occur elsewhere, but a second XML declaration is precluded by virtue of the definition of PITarget and this statement:

目标名称"XML "、"xml "等保留用于本规范的此版本或未来版本中的标准化.

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

这篇关于文档中的多个 XML 声明是否是格式良好的 XML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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