Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are multiple XML declarations in a document well-formed 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.

From Extensible Markup Language (XML) 1.0

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

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.

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

like image 930
Mike Avatar asked Nov 27 '13 19:11

Mike


People also ask

What is the XML declaration?

The xml declaration simply declares that a document is an xml document and describes its version. It is optional, but if you use it (and by convention you should, unless you are working with a document fragment for inclusion in another xml document), it must unequivocally, be the first statement in an xml document:

What is a well formed XML document?

Well Formed XML. Introduction. An XML document is called well-formed if it satisfies certain rules, specified by The W3C. These rules are: A well-formed XML document must have a corresponding end tag for all of its start tags. Nesting of elements within each other in an XML document must be proper.

What are the rules for XML document?

These rules are: A well-formed XML document must have a corresponding end tag for all of its start tags. Nesting of elements within each other in an XML document must be proper. In each element two attributes must not have the same value. Markup characters must be properly specified. An XML document can contain only one root element.

How do you know if an XML is valid?

Valid XML Document. If an XML document is well-formed and has an associated Document Type Declaration (DTD), then it is said to be a valid XML document. We will study more about DTD in the chapter XML - DTDs.


1 Answers

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.

like image 56
kjhughes Avatar answered Nov 03 '22 19:11

kjhughes