Element sorting for XSD-based elements

An XSD can define the strict order of children of an element. For non-XSD elements, element order is undefined.

Each XmlElement exposes a Children property. For XSD-based elements, the property name is $Children.

If the list of child elements is out of order according to the XSD, Gosu sorts the element list during serialization to match the schema. This sorting does not affect the original order of the elements in the content list.

If you use APIs to directly add child elements, such as adding to the child element list or using an addChild method, you can add child elements out of order. Similarly, some APIs indirectly add child elements. For example, this occurs when intermediary elements are automatically created. In all of these cases, Gosu permits the children to be out of order in the XmlElement object graph.

During serialization, Gosu sorts the elements to ensure that the elements conform to the XSD.

Note that if you use an XSD when you parse XML into an XmlElement, the elements must be in the correct order according to the XSD. If the child order violates the XSD, Gosu throws an exception.

Example 1

The following XSD defines three ordered child elements for a complex XML type.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Element1">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="Child1" type="xsd:int"/>
        <xsd:element name="Child2" type="xsd:int"/>
        <xsd:element name="Child3" type="xsd:int"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

The following code assigns values to the child elements of the parent element:

var xml = new schema.Element1()
xml.Child2 uses examples.pl.gosu.xml.myschema.Element1

var xml = new Element1()
xml.Child2 = 2
xml.Child1 = 1
xml.Child3 = 3
xml.print()

This code prints the following:

<?xml version="1.0"?>
<Element1>
  <Child1>1</Child1>
  <Child2>2</Child2>
  <Child3>3</Child3>
</Element1>

Example 2

The following XSD defines two sets of ordered child elements for a complex XML type. Both sets include some of the same child elements, but use a different ordering.

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="Element1">
    <xsd:complexType>
      <xsd:choice>
        <xsd:sequence>
          <xsd:element name="A" type="xsd:int"/>
          <xsd:element name="B" type="xsd:int"/>
          <xsd:element name="C" type="xsd:int"/>
        </xsd:sequence>
        <xsd:sequence>
          <xsd:element name="B" type="xsd:int"/>
          <xsd:element name="C" type="xsd:int"/>
          <xsd:element name="A" type="xsd:int"/>
          <xsd:element name="Q" type="xsd:int"/>
        </xsd:sequence>
      </xsd:choice>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

The following code assigns values to the child elements of the parent element:

uses examples.pl.gosu.xml.myschema.Element1

var xml = new Element1()
xml.A = 5
xml.B = 5
xml.C = 5
xml.print()
print( "----------" )
xml.Q = 5
xml.print()

This code prints the following:

<?xml version="1.0"?>
<Element1>
  <A>5</A>
  <B>5</B>
  <C>5</C>
</Element1>
----------
<?xml version="1.0"?>
<Element1>
  <B>5</B>
  <C>5</C>
  <A>5</A>
  <Q>5</Q>
</Element1>

See also