Does XML care about the order of elements?

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.)

Leave a Comment