title: Is there a way to enforce/preserve order of XML elements in an XML Schema?
body: <p>Let's consider the following XML Schema:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;schema 
      targetNamespace="http://www.example.org/library" 
      elementFormDefault="qualified" 
      xmlns="http://www.w3.org/2001/XMLSchema" 
      xmlns:lib="http://www.example.org/library"&gt;
  
      &lt;element name="library" type="lib:libraryType"&gt;&lt;/element&gt;
  
      &lt;complexType name="libraryType"&gt;
          &lt;sequence&gt;
              &lt;element name="books" type="lib:booksType"&gt;&lt;/element&gt;
          &lt;/sequence&gt;
      &lt;/complexType&gt;
  
      &lt;complexType name="booksType"&gt;
          &lt;sequence&gt;
              &lt;element name="book" type="lib:bookType" 
                       maxOccurs="unbounded" minOccurs="1"&gt;&lt;/element&gt;
          &lt;/sequence&gt;
      &lt;/complexType&gt;
  
      &lt;complexType name="bookType"&gt;
          &lt;attribute name="title" type="string"&gt;&lt;/attribute&gt;
      &lt;/complexType&gt;
  &lt;/schema&gt;
  </code></pre>
  
  <p>and a corresponding XML example:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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 "&gt;
  
    &lt;lib:books&gt;
      &lt;lib:book title="t1"/&gt;
      &lt;lib:book title="t2"/&gt;
      &lt;lib:book title="t3"/&gt;
    &lt;/lib:books&gt;
  
  &lt;/lib:library&gt;
  </code></pre>
  
  <p>Is there a way to guarantee that the order of <code>&lt;lib:book .../&gt;</code> 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 <code>title="t1"</code>, then the book with <code>title="t2"</code>, and finally the book with <code>title="t3"</code>.</p>
  
  <p>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 <code>index</code> attribute to the <code>&lt;lib:book .../&gt;</code> element, and delegate order preservation to the application reading the XML.</p>
  
  <p>Comments? Suggestions?</p>