Mark
Mark

Reputation: 11740

Does the sequence schema element guarantee order of child elements?

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

Upvotes: 9

Views: 5219

Answers (4)

WaiverWireAddict
WaiverWireAddict

Reputation: 3

Your question is really two questions.

  • Does sequence affect ordering of child elements?
  • Why are two distinct XML fragments both valid as far as the schema is concerned when the order of their data values differ?

Does sequence affect ordering of child elements?

Yes, in an XSD sequence is used to specify a set of child elements where all of the child elements must be part of the parent complexType, and that the order of the child elements must be the order that they occur in the sequence definition.

all is used to specify a set of child elements where all of the child elements must be part of the parent complexType, and that the order of the child elements is not important. The child elements can occur in any order in a potential .xml file.

The problem in trying to understand how sequence works in your example is that the example sequence has only 1 child element. You need at least 2 child elements for the concept of order to be relevant.

When there is only one child element, both all and sequence are equivalent.

BlueToque's answer tries to clarify this distinction by introducing a 2nd child element to the sequence.

Why are two distinct XML fragments both valid as far as the schema is concerned when the order of their data values differ?

An XSD describes the structure of an XML file, and possibly restrictions on valid elements. The ordering that sequence describes is constrained to the structure of the XML elements.

The XSD sees both of the XML fragments as 3 string elements part of a parent ArrayOfstring. The structure is the same. The ordering of the structure is the same.

An XSD can constrain the valid data values of an element, but an XSD does not have the ability to constrain the ordering of the data values of elements.

Upvotes: 0

sylvanaar
sylvanaar

Reputation: 8216

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

Upvotes: 2

Blue Toque
Blue Toque

Reputation: 1804

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.

Upvotes: 7

Welbog
Welbog

Reputation: 60418

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.

Upvotes: 1

Related Questions