Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does XML care about the order of elements?

Tags:

xml

xsd

XML confuses me sometimes, but I'm trying to get this figured out. What the vendor is telling me doesn't make sense but again, XML and I don't get along :)

I have some XML that I'm sending to a vendor's web service that is giving me random failures:

<root>     <Request>         <Driver id="1" VehId="1">...</Driver>         <Driver id="2" VehId="1">...</Driver>         <Driver id="3" VehId="2">...</Driver>         <Vehicle id="1">...</Vehicle>         <Vehicle id="2">...</Vehicle>         <Driver id="4" VehId="2">...</Driver>     </Request> </root> 

There is no XSLT or XSD to compare against to see if my XML is valid.

The vendor states that the XML is invalid because Driver #4 is in the wrong area. The XPath for Driver should be root/Request/Driver and Vehicle is root/Request/Vehicle.

Is it common that XML parsers would force an element order, especially if there is no XSD to compare the XML to? The vendor's support is slow to get back with me, so I want to know what good common practice is.

Followup

I complained enough to our account rep about not being able to test this (and made it sound like they were just trying to get support money) that it turns out that the Developers have the XSD, but Support doesn't. So I had been talking to the wrong group *facepalm*

I got the XSD, and it does enforce a specific order of elements.

Now to fight them in regards to their own sample XML doesn't follow the schema, but at least now I have something to test against.

like image 581
dragonmantank Avatar asked Dec 01 '10 20:12

dragonmantank


People also ask

Does XML maintain order?

XML element comparison by default preserves the order of elements in the two documents. A change of order will therefore result in elements appearing as added and deleted.

Are XML attributes ordered?

XML attributes are re-ordered alphabetically by the DOM parser. Attributes are being displayed in alphabetical order suggesting that the parser is sorting them.

What is sequence in XML?

The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.


1 Answers

XML schema compositor "sequence" will enforce ordering

I know this is old but I just came upon the post.

Until today I would most likely answer the question Does XML care about the order of elements? with No, unless you use a poorly written xml parser.

However, today a third party application complained that the xml files I created were invalid. They use an XSD file to validate the xml. And yes, you can enforce the order or elements within an xsd file:

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">   <xs:complexType name="ComplexType">     <xs:sequence>       <xs:element minOccurs="0" maxOccurs="1" default="" name="Value1" type="xs:string" />       <xs:element minOccurs="0" maxOccurs="1" default="" name="Value2" type="xs:string" />     </xs:sequence>   </xs:complexType> </xs:schema> 

The keyword is xs:sequence

The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

which is in contrast to xs:all which does not care about the order but only allows elements which occur zero or once.

Specifies that the child elements can appear in any order. Each child element can occur 0 or 1 time

(The words sequence and all are both what is called a Compositor in the XML Schema definition.)

like image 108
Jürgen Steinblock Avatar answered Oct 25 '22 07:10

Jürgen Steinblock