Search code examples
xmlxsd

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


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?


Solution

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