Are multiple XML declarations in a document well-formed XML?

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

Must be at the top

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

[1]     document ::= prolog element Misc*

Then check prolog:

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

And then XMLDecl:

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

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

Only one

Processing instructions

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

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

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

Leave a Comment