Why is node important for ordering in XML?

I recently dealt with an API that requires the nodes of an XML document to be in a specific order. I was wondering why they feel the need to provide this when I cannot find absolutely no reason why everything should be so.

For intance, this will be correct (xml is greatly simplified)

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

but it will return an error

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

At first it seemed to me that they can store things in the form of a list / array, and indexes always refer to the same node. I understand why sending host nodes having the same name in the same order is important as described in this question . However, some nodes may be omitted:

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

So, in the third example, the quantity and order will now be in [0] and [1] instead of [1] and [2] in the first (correct) example.

Another thought would be that they treat XML as a string and require that they always know that nodes come one after another, but again, since nodes can be deleted, this theory makes no sense.

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

+6
source share
5 answers

Node order obviously matters in XML:

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

This is less obvious in XML, like what you provided, which looks like some kind of serialization format. But objects whose properties have side effects can fail if the properties are not set in the correct order.

Imagine a class with a private Person that exposes the PersonID and Name properties. The PersonID tool creates a private instance of Person , and setter Name sets the Name property in the private Person . In this case, setting Name before setting PersonID fails because Person does not exist yet.

This is the case when the implementation of the scheme, which requires that the PersonID appear before Name in XML, this error does not occur due to the fact that other developers are forced to do things that seem to be meaningless.

Obviously, in such situations, you can find a developer who wrote this class and beat it. It is rarely possible, although it is interesting to contemplate the world in which he was.

+15
source

One of the reasons an XML node order might be required is where the application uses a streaming parser. Having dependent elements in the expected order can allow the application to significantly increase the efficiency of XML data processing. This is especially true for applications that process large-scale XML data.

+5
source

The answer lies with XML-DTD / Schema . The original schema defined in the API results in an error. Although I believe that I do not want to teach XML here, still look at the following so that everything is clear.

XML has two points to consider:

  • Well-formed XML: great syntax
  • Valid XML: excellent against DTD (Document Type Definition) / Schema

About DTD Points: Suggested DTD for your question:

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

The above suggestion is a DTD for the structure you provided in the question. Since you are dealing with a specific API, it has the type of structure that is already defined in it. An 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, the XML schema is used instead of DTDs because they define the data structure for their users much better and provide an object approach. .

+2
source

This is faster and easier for code depending on the order of the elements.

It can also prevent some ambiguity problems when an order is allowed for arbitrary.

In addition, XML is not intended for human readers because it is intended for use by computer programs. Computers don't mind doing things in order.

+1
source

Forced ordering makes it easy for the consumer to look like this:

 consumeTransation: consumeAddressIfPresent; consumeAmountIfPresent; consumeOrderIDIfPresent; 

Moreover, using XML Schema to define the structure makes ordering a more likely requirement . This is because XML Schema has richer support for ordered lists ( xs:sequence ), which is for unordered lists ( xs:all ). The latter have entry restrictions that are more difficult to verify and are not extensible in the sequence of sequences. Some of them are improved in XML Schema 1.1, but most tools / APIs do not exist yet.

+1
source

Source: https://habr.com/ru/post/897488/


All Articles