Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the tag order relevant for valid XML?

in other words:

<?xml version="1.0" encoding="UTF-8"?>
<root>
 <tag1>
  <tag1.1></tag1.1>
 </tag1>
 <tag2 />
<root>

is the same as:

<?xml version="1.0" encoding="UTF-8"?>
    <root>
     <tag2 />
     <tag1>
      <tag1.1></tag1.1>
     </tag1>     
    <root>

?

like image 391
Segolas Avatar asked Nov 05 '10 11:11

Segolas


People also ask

Does order of tags matter in XML?

According to the XML specification, the order of attribute specifications in a start-tag or empty-element tag is not significant.

What makes a valid XML document?

Well Formed XML DocumentsXML documents must have a root element. XML elements must have a closing tag. XML tags are case sensitive. XML elements must be properly nested.

Are XML attributes ordered?

XML attributes are re-ordered alphabetically by the DOM parser. Attributes are being displayed in alphabetical order suggesting that the parser is sorting them.


1 Answers

In as much detail as they are shown, your two examples are semantically the same.

It's a common misunderstanding of XML that in a well-formed XML document the order of sibling elements is significant. The XML 1.0 spec specifies that attributes are unordered, however it says nothing about elements. Therefore an XML processor is free to report element siblings in any order it likes.

That said, I don't think any commonly used XML processors report elements in a different order to the order they appear in a document.

You ask about a 'valid' document – this implies that a DTD or schema is in use, and therefore it may (or may not) be the case that the order is relevant. There are mechanisms for a DTD or schema to specify that element order is relevant within a document. However, your examples don't show use of a DTD or schema.

like image 158
philiphobgen Avatar answered Sep 22 '22 07:09

philiphobgen