Order of tags

cjard

Well-known member
Joined
Apr 25, 2006
Messages
7,081
Programming Experience
10+
I recently had to make a case that we should never rely on the order of appearance of tags in an XML document to have any significant or special meaning. I was basing my argument on the fact that we cannot be sure in what order a document will be parsed (breadth/depth first or even forwards/backwards) thus to say "the first Person tag must be the husband, the second must be the wife" is a foolish thing to do with XML.

Does anyone else hold this notion, that without an attribute indicating some kind of sequence, we must never regard the top-down order to mean anything?
 
Your example is two nodes of otherwise equal rank (level and node name), like this:
HTML:
<Persons>
   <Person id="3"/>
   <Person id="2"/>
</Persons>
With this I agree, the Xml structure here cannot determine any schematic difference between the nodes and cannot rule a specific order, a document reader could still read the attribute value and distinguish them. The reference is the language that is defined to validate Xml, Xml Schema Definition language (XSD). Person can only be validated with occurence here as an array, the order of elements can not be validated with Xsd. In the example you stated the Xml structure would have to define <husband> and <wife> nodes, both could be of schema type Person.

For another case the order could be important, this would be where Xsd define a sequence. Even though Firstname and Lastname here are two simple nodes that are both childnodes of Person they have different names and are therefore part of the Xml Structure that can be validated with a Xsd schema as a sequence where the order must be as the schema says. Also here any Xml reader will be able to read this content blindly when disregarding the schema order of Firstname or Lastname.
HTML:
<Person>
   <Firstname/>
   <Lastname/>
</Person>
The schema can here be written differently, the Person complex type childs doesn't have to be a sequence.

Also note that .Net Xml tools does preserve array order both when reading a document, making selections of nodes (lookups), and serializing out arrays to Xml.
 
Also note that .Net Xml tools does preserve array order both when reading a document, making selections of nodes (lookups), and serializing out arrays to Xml.

Handy to know.. THis harks back to a conference call that I had with experian, whereby I pass 2 addresses for verification of a person's identity. I wanted to know how they would understand that one address was the current, and one address was the previous. They said that it was purely down to the order with which the addresses were presented within the document. I was less than impressed. However, knowing this info I can at least change my xml classes so that the CUrrentAddress and PreviousAddress properties point to array indexes ADDR[0] and ADDR[1] respectively, which should guarantee order of output.
 
Back
Top