12 Sept 2008

Is node order important in xml ?

"Is node order important in xml ?"

This has always been a pending question, I wanted to answer.

The quick answer:
  • No dtd, xml schema(xsd) validation, node order doesn't matter
  • Any type of xml validation, order does matter (unless using a special notation in the schema)
The long answer:

For a long time I was pretty sure the order wasn't important, you could write:

<root>
<node1/>
<node2/>
</root>

and

<root>
<node2/>
<node1/>
</root>

It was the same...

But when using eclipse and validating an xml document, the validator kept complaning if the node were not in the same order as describe in the xsd.

I didn't know if it was an eclipse validator limitation (the validator wasn't able to cop with node order) or a w3c xml requirement.

Untill I found this IBM page describing the same exact issue ;)
Principles of XML design: When the order of XML elements matters

Apparently you can specify in an relax ng schema that order doesn't matter (using ampersand "&") (see: chapter "Schema constraints of element order") but I suspect many people (me included) to use the easiest path and use the comma notation.
As a side effect that means the order does matter, but people writing schema usually don't do this on purpose...


Technorati tags:

4 comments:

Anonymous said...

The importance of node order depends on the nature of the XML document.

Take an HTML document:
  <body>
    <h1>Section title</h1>
    <p>The text</p>
  </body>
and
  <body>
    <p>The text</p>
    <h1>Section title</h1>
  </body>
are quite different, but a for a purchase order
  <order>
    <item>5379134</item>
    <price>15.95</price>
  </order>
and
  <order>
    <price>15.95</price>
    <item>5379134</item>
  </order>
are equivalent.

So node order really depends on what the document represents. And the DTD/Schema/Relax should reflect this (although DTD can't express this).

Benjamin Francisoud said...

True :)
I wouldn't like the head tag to be beblow the body one in this case :o

Anonymous said...

@Anonymous: In your example it's an instance of a rule set. The rule set itself does not specify the order of elements, like an a tag is only valid after all your div are placed. This, goes, of course with some exceptions like the order of the html and body tag. I came here after working with an XML document where it was important to put the house number tag before the street name tag inside an address tag and much more. This does not make XML very usable...

osm18 said...
This comment has been removed by a blog administrator.