Is there a way to enforce/preserve order of XML elements in an XML Schema?

Is there a way to guarantee that the order of elements is preserved? I want to be sure that any parser reading the XML will return books in the specified oder, that is first the book with title="t1" , then the book with title="t2" , and finally the book with title="t3" . As far as I know XML parsers are not required to preserve order. I wonder whether one can enforce this through XML Schema? One quick solution for me would be adding an index attribute to the element, and delegate order preservation to the application reading the XML. Comments? Suggestions?

asked May 26, 2010 at 10:07 13.5k 7 7 gold badges 43 43 silver badges 64 64 bronze badges

You wrote "As far as I know XML parsers are not required to preserve order". From where did you get that? That's not true for elements. In fact the xs:sequence enforce the order for a validation schema.

– user357812 Commented Apr 3, 2011 at 4:22

Sequence has nothing to do with a repeated element, but with a disparate group of child elements which must occur in a specific order.

Commented Jun 30, 2014 at 22:09

5 Answers 5

According to Michael Kay who seems to be important person in XML world the order is preserved.

Yes, you can assume that the order of elements will be preserved. The writers of the XML specification neglected to say this explicitly, but that's because they thought it was obvious. A parser that didn't preserve order might be technically conformant, but no-one would ever use it; many, many XML applications depend on order being preserved, notably those that use XML to represent documents.

answered Nov 9, 2014 at 14:22 user1121956 user1121956 1,923 2 2 gold badges 21 21 silver badges 34 34 bronze badges

As mentioned in a comment above, xs:sequence defines an ORDERED collection. Here is the proof:

. These elements must be called name, street, city, state and zip as specified by the values of the declarations' name attributes, and the elements must appear in the same sequence (order) in which they are declared.

30.8k 15 15 gold badges 72 72 silver badges 104 104 bronze badges answered Aug 30, 2012 at 12:21 49 3 3 bronze badges

The author of the question is asking about preserving the order of repeating elements such as , not the order of different elements

Commented Feb 11, 2014 at 2:51

I know this is very old, but none-the-less, I am also looking for a way to guarantee a preserved order of XML elements.

While all current parsers might preserve order, the XML definition in no way requires this, and future parsers might handle the order completely differently.

It seems the best solution that exists right now is to give the elements an 'id' attribute so that ordering can be verified in code after being parsed.

I hate having to require my users to add an arbitrary , , etc. when the order is already obvious in the XML file, but as far as I know there is no official standard in XML for requiring a specific order for repeating elements.

Update

If you're using .NET, it has a property you can set to define which order it reads such elements in. So, I guess as long as you know where your XML will be parsed, and the parser supports the enforcement of element ordering the way you require, then order can be enforced.

If, however, a third party were to take and try to replicate your results with their own parser, confusion could ensue since that order is configured somewhere else, not within the XML, therefore no other parser implementation would know to utilize that order.

So, this can all be summarized that: (a) Order cannot be enforced within the XML specification itself, but (b) Order can be and often is enforced by a large number of XML parsers out there.