在文档格式良好的XML中有多个XML声明吗?

在同一个文档格式良好的 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声明,并且它必须位于顶部(如果有的话).

必须在顶部

请参阅XML RecommendationWell-Formed XML Documents部分中的文档定义:

[1]     document ::= prolog element Misc*

然后检查prolog:

[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.

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念