9

Let's consider the following XML Schema:

<?xml version="1.0" encoding="UTF-8"?>
<schema 
    targetNamespace="http://www.example.org/library" 
    elementFormDefault="qualified" 
    xmlns="http://www.w3.org/2001/XMLSchema" 
    xmlns:lib="http://www.example.org/library">

    <element name="library" type="lib:libraryType"></element>

    <complexType name="libraryType">
        <sequence>
            <element name="books" type="lib:booksType"></element>
        </sequence>
    </complexType>

    <complexType name="booksType">
        <sequence>
            <element name="book" type="lib:bookType" 
                     maxOccurs="unbounded" minOccurs="1"></element>
        </sequence>
    </complexType>

    <complexType name="bookType">
        <attribute name="title" type="string"></attribute>
    </complexType>
</schema>

and a corresponding XML example:

<?xml version="1.0" encoding="UTF-8"?>
<lib:library 
    xmlns:lib="http://www.example.org/library" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.example.org/library src/library.xsd ">

  <lib:books>
    <lib:book title="t1"/>
    <lib:book title="t2"/>
    <lib:book title="t3"/>
  </lib:books>

</lib:library>

Is there a way to guarantee that the order of <lib:book .../> 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 <lib:book .../> element, and delegate order preservation to the application reading the XML.

Comments? Suggestions?

MarcoS
  • 13,386
  • 7
  • 42
  • 63
  • 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. –  Apr 03 '11 at 04: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. – Lawrence Dol Jun 30 '14 at 22:09

4 Answers4

9

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.

Source: http://lists.xml.org/archives/xml-dev/201003/msg00045.html

user1121956
  • 1,843
  • 2
  • 20
  • 34
4

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

<xsd:complexType name="USAddress">
   <xsd:sequence>
    <xsd:element name="name"   type="xsd:string"/>
    <xsd:element name="street" type="xsd:string"/>
    <xsd:element name="city"   type="xsd:string"/>
    <xsd:element name="state"  type="xsd:string"/>
    <xsd:element name="zip"    type="xsd:decimal"/>
   </xsd:sequence>
   <xsd:attribute name="country" type="xsd:NMTOKEN" fixed="US"/>
</xsd:complexType>

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

Source: W3C: XML Schema Part 0

bstpierre
  • 30,042
  • 15
  • 70
  • 103
shuron
  • 49
  • 3
  • 4
    The author of the question is asking about preserving the order of repeating elements such as ``, not the order of different elements – Tom Howard Feb 11 '14 at 02:51
2

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 id="1", id="2", 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.

Ben
  • 62
  • 6
  • 4
    I disagree; the XML world is rife with examples of where the order of repeated elements is critical (XHTML for example; the browser can't render the paragraphs of an article in any old order). More importantly the XML Schema for a `` is broken if one can't assume that the document order of the child `` elements is preserved. So a parser which didn't preserve the order of repeated elements could not read the schema required to validate a document. Other schema tags like `` and `` are likewise sensitive to order. – Lawrence Dol Jun 30 '14 at 22:12
  • This was my understanding in 2011 based on reading the specifications. Most parsers do maintain the order of all elements, and obviously in a more specialized type of XML such as XHTML as defined by the W3C, document order is maintained. However I do know that the XML 1.0 specification does not guarantee element order itself. I do think xs:sequence is likely your best bet for an element type that should maintain order--it seems in most places that I read that maintaining order is at least implied. – Ben Jul 01 '14 at 13:40
  • 1
    It seems to my reading that `xsd:sequence` is a requirement to preserve the order of disparate elements, not repeated elements. Conversely there seems to be a ubiquitous assumption that repeated elements are to be delivered in document order. But it wouldn't be the first screwup in the world of XML and SOAP, and it surely won't be the last. – Lawrence Dol Jul 03 '14 at 16:31
  • most xml parsers (if not all) preserve the order or elements. In fact, order of elements are conceptually an absolute must for many xml based standard vocabularies, a prominent one being xhtml. It is also implicitly assumed by XSD schema definitions. So, your answer is simply wrong – Kemal Erdogan Jun 07 '17 at 13:32
  • Whether or not some (or even most) parsers assume things does not matter. Element order is context dependent, and parser-dependent, much of the time. XHTML has its own specification. XSD has its own specification. Those are not relevant to a discussion of the base XML specification. My conclusion was that it's not in the spec, but most parsers enforce order anyway. What is wrong about that? – Ben Jun 07 '17 at 17:19
1

The order of elements (unlike attributes) is significant in XML and every parser I know will preserve it.

unbeli
  • 29,501
  • 5
  • 55
  • 57
  • 1
    Reading this [article](http://www.ibm.com/developerworks/xml/library/x-eleord.html) I thought that was not the case. In fact it says "The XML 1.0 well-formedness definition specifically states that attributes are unordered, but says nothing about elements. This means that technically speaking, a conforming XML parser might decide to report the child elements of memo in Listing 1 in any order." – MarcoS May 26 '10 at 12:22
  • 2
    Every parser is free to order the elements as it like. The only way to enforce the order is by using a sequence in an xsd. – Fedearne Nov 10 '11 at 21:11
  • The order of ***disparate*** elements is not preserved; the order of a repeated element must surely be preserved or else XML has no representation of a simple list. – Lawrence Dol Jun 30 '14 at 22:14