PaulB
PaulB

Reputation: 24372

Should I use the position of XML elements to detemine their order?

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>

Upvotes: 2

Views: 183

Answers (2)

sleske
sleske

Reputation: 83577

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.

Upvotes: 0

Tomalak
Tomalak

Reputation: 338118

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.

Upvotes: 1

Related Questions