Is there a way to guarantee that the order of
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:22Sequence 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:09According to Michael Kay who seems to be important person in XML world the order is preserved.
answered Nov 9, 2014 at 14:22 user1121956 user1121956 1,923 2 2 gold badges 21 21 silver badges 34 34 bronze badgesYes, 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.
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 badgesThe author of the question is asking about preserving the order of repeating elements such as
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.