Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the sequence schema element guarantee order of child elements?

Tags:

xml

schema

xsd

Given this xml schema (fragment):

<xs:element name="GetIEnumerableResponse">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="0" name="GetIEnumerableResult" nillable="true" xmlns:q4="http://schemas.microsoft.com/2003/10/Serialization/Arrays" type="q4:ArrayOfstring" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

In this xml fragment:

<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <string>string1</string>
  <string>string2</string>
  <string>string3</string>
</ArrayOfstring>

can the <string></string> elements occur in any order? Thus, are these two fragments of XML semantically equvalent:

<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <string>string1</string>
  <string>string2</string>
  <string>string3</string>
</ArrayOfstring>

<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
  <string>string3</string>
  <string>string2</string>
  <string>string1</string>
</ArrayOfstring>

Or does the sequence element in the schema mean the <string></string> elements have to occur in the same order to be semantically equivalent?

Does the presence of the in the schema require the parser/deserializer to keep the elements in the order they exist in the xml text? If I understand correctly, normally (i.e. without a schema) there is no requirement to do so (even if most parsers usually do).

like image 938
Mark Avatar asked Aug 26 '09 17:08

Mark


People also ask

Does the Order of the elements in a schema matter?

When defining your schema with XSD, you can specify whether or not order matters. When you use the xs:sequence indicator, you are saying that the order of the elements matters, and it should follow the order specified. Fro XSD 1.0 implementations, this also means that each child element can occur from 0 to any number of times (unbounded).

What is a sequence element?

Definition and Usage. The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

What is the Order in which child elements appear in XML?

Generally speaking, the order in which child elements appear inside their parent element container in XML shouldn't matter. As such, the following two examples would be considered semantically equivalent XML.

How many times can a child element occur in a sequence?

Each child element can occur from 0 to any number of times. (The ? sign declares that the element can occur zero or one time inside the sequence element) Optional. Specifies a unique ID for the element Optional. Specifies the maximum number of times the sequence element can occur in the parent element.


2 Answers

The Sequence element means that the individual elements (not the elements in the array) are supposed to preserve order.

For instance

<xs:element name="GetIEnumerableResponse">
  <xs:complexType>
    <xs:sequence>
      <xs:element minOccurs="0" name="GetIEnumerableResult" nillable="true" xmlns:q4="http://schemas.microsoft.com/2003/10/Serialization/Arrays" type="q4:ArrayOfstring" />
      <xs:element name="Dummy" type="xs:string" />
    </xs:sequence>
  </xs:complexType>
</xs:element>    

In this example, Dummy appears after GetIEnumerableResult in the sequence, and to validate it should always appear in this order in this complex type.

The order of "repeating" items in the "ArrayOfString" complex type is not enforcable in the schema, and because arrays do not imply or enforce any explicit order the semantics of order are not guaranteed in the CLR either.

One way to guarantee order would be to impose order on the collection by serializing an index.

like image 194
Blue Toque Avatar answered Sep 29 '22 01:09

Blue Toque


The string elements can occur in any order - since to the schema they are all the same

like image 30
sylvanaar Avatar answered Sep 29 '22 01:09

sylvanaar