Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is node order important in XML?

Tags:

xml

xsd

I've been dealing with an API recently that requires nodes of the XML document to be in a particular order. I was wondering why they feel the need to enforce this when I can find absolutely no reason why things should be like this.

For intance, this would be correct (xml greatly simplified)

<transaction>
    <address>1 main st</address>
    <amount>100</amount>
    <orderId>1234</orderId>
</transaction>

but this would return an error

<transaction>
    <address>1 main st</address>
    <orderId>1234</orderId>
    <amount>100</amount>
</transaction>

At first I thought it would be so that they can store things in a list/array form and have the indices always refer to the same node. I understand why sending sibling nodes that have the same name in the same order is important as explained in this question. However, some nodes can be left out:

<transaction>
    <amount>100</amount>
    <orderId>1234</orderId>
</transaction>

So in the third example, amount and orderId would now be at [0] and [1] instead of at [1] and [2] in the first (correct) example.

Another thought would be that they process the XML as a string and require that they always know what nodes come after each other, but again since omitting nodes is allowed, that theory doesn't make sense.

Can anyone explain to me why the order in which I give nodes matters? Or am I just dealing with an API that is old and grumpy?

like image 888
helloandre Avatar asked Sep 16 '11 18:09

helloandre


3 Answers

Node order obviously matters in XML like this:

<p>
   <span>This wouldn't make much sense</span>
   <span>if the order of these nodes were reversed.</span>
</p>

It's less obvious in XML like what you provided, which appears to be some kind of serialization format. But objects whose property setters have side effects can fail if the properties aren't set in the right order.

Imagine a class with a private Person field that exposes PersonID and Name properties. The PersonID setter creates the private instance of Person, and the Name setter sets the Name property on the private Person field. In this case, setting Name before you set PersonID fails, because Person doesn't exist yet.

This is a case where implementing a schema that requires PersonID to appear before Name in the XML keeps this error from happening, at the cost of forcing other developers to do things that are apparently nonsensical.

Tthe obvious thing to do in situations like this is to find the developer who wrote that class and beat him. This is rarely possible, though it's interesting to contemplate a world in which it were.

like image 107
Robert Rossney Avatar answered Sep 21 '22 13:09

Robert Rossney


One reason XML node order can matter is where an application is using a streaming parser. Having dependent elements in the expected order can allow the application to be significantly more efficient processing the XML data. This is especially true for applications processing large scale XML data.

like image 25
Jim Moore Avatar answered Sep 19 '22 13:09

Jim Moore


The answer lies in XML-DTD/Schema. The underlying schema so defined in the API results in the error. Though I suppose I donot wish to teach XML here, still a look at the following will make things clear.

XML has two points to be considered:

  • Well Formed XML: perfect syntax
  • Valid XML: perfectly valid against a DTD (Document Type Definition) / Schema

Points about DTD: Suggested DTD upon your question:

<!DOCTYPE transaction
[
<!ELEMENT address (#PCDATA)>
<!ELEMENT amount (#PCDATA)>
<!ELEMENT orderid (#PCDATA)>
]>

The above is a suggested DTD upon the structure you provided in the question. Since you are dealing with a particular API, it has such type of structure already defined in it. Alternative to this is the XML schema.

Points about XML Schema:

<xs:element name="transaction">    
<xs:complexType>
  <xs:sequence>
    <xs:element name="address" type="xs:string"/>
    <xs:element name="amount" type="xs:string"/>
    <xs:element name="orderid" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
</xs:element>

Currently, XML schema are used instead of DTDs as they are far more superior in defining the data structure for their users and provide an object-oriented approach.

like image 20
jagbandhuster Avatar answered Sep 19 '22 13:09

jagbandhuster