12 Sep 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:

2 comments:

Sylvain Wallez 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