views:

409

answers:

3

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"&gt;
  <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"&gt;
  <string>string1</string>
  <string>string2</string>
  <string>string3</string>
</ArrayOfstring>

<ArrayOfstring xmlns="http://schemas.microsoft.com/2003/10/Serialization/Arrays"&gt;
  <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).

A: 

It really depends on the context. In XML's purest form, the snippets you have provided are semantically equivalent regardless of order. But when you're (de)serializing things with XML, there might be meaning associated with the order of the elements.

In this case, the XML documents are semantically equivalent if and only if the resulting arrays are semantically equivalent.

Welbog
Right, that's the question. Does the presence of the <sequence> 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).
Mark
As I understand it the schema has no bearing on how the elements are read. Rather it's just a method to know whether a document is valid or not while reading it.
Welbog
+2  A: 

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.

Oplopanax
Excellent answer, and serializing an index is indeed the original solution I used. I created an "OrderedList<T>" class that was essentially an dictionary of <int,T> where the int was the index. Apparently that was the right answer.
Mark
+1  A: 

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

sylvanaar