tags:

views:

50

answers:

1

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"&gt;

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

A: 

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

unbeli
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