tags:

views:

39

answers:

2

Say I've got a snippet of XML

<Items>
  <SubItem Name="Banana">
  <SubItem Name="Apple">
<Items>

Is it valid to say that Banana comes before Apple? I know it does syntactically - but does it semantically.

Or do I need to include some ordering attribute:

<Items>
  <SubItem Name="Banana" Index="0">
  <SubItem Name="Apple" Index="1">
<Items>
A: 

There is something in XML (XPath, really) called "document order". So yes, document order is a valid thing you can rely on.

It can change only if the document changes. No "ordering attribute" needed unless you cannot guarantee that the document is being built in the right order.

Tomalak
A: 

Yes, you can generally rely on the order of elements in your document. XML processing tools should respect it when they transform and parse your document. It would probably still be a good idea to check.

Note, BTW, that attribute order (order of attributes inside an element) is not guaranteed by the standard.

sleske