Does XML care about the order of elements?
Solution 1:
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.)
Solution 2:
If there is no XSD (XML schema) at hand, then all you can check for is whether or not your XML is well formed.
In your case - it is. There are no overlapping XML tags, no XML tag that are left open or anything of that sort.
If the vendor needs to enforce things like order inside the XML, he ought to provide an XSD file - otherwise, his "requirements" cannot be validated and checked....
Solution 3:
Vendors will do what they will do, but it is a nonstandard application of XML to rely on ordering.
XML is declarative, not procedural. So, it shouldn't be "stepwise".