Re: xml schema question

Hi Peter,

> The elements aa and bb are required to be there and they have to
> occur exactly once. Other sub elements of element can occur many
> times. Especially the order of the elements is random, so i may not
> use xsd:sequence.

This is one of the areas where XML Schema isn't very helpful because
the kind of model group that allows elements to occur in any order
(<xs:all>) only allows those elements to be optional or appear exactly
once.

The closest that you can get is to use a content model like:

  (otherElements*, ((aa, otherElements*, bb) |
                    (bb, otherElements*, aa)), otherElements*)

Use a model group to define otherElements. For example:

<xs:group name="otherElements">
  <xs:choice>
    <xs:element ref="cc" />
    <xs:element ref="dd" />
  </xs:choice>
</xs:group>

<xs:element name="element">
  <xs:complexType>
    <xs:sequence>
      <xs:group ref="otherElements" minOccurs="0"
                                    maxOccurs="unbounded" />
      <xs:choice>
        <xs:sequence>
          <xs:element ref="aa" />
          <xs:group ref="otherElements" minOccurs="0"
                                        maxOccurs="unbounded" />
          <xs:element ref="bb" />
        </xs:sequence>
        <xs:sequence>
          <xs:element ref="bb" />
          <xs:group ref="otherElements" minOccurs="0"
                                        maxOccurs="unbounded" />
          <xs:element ref="aa" />
        </xs:sequence>
      </xs:choice>
      <xs:group ref="otherElements" minOccurs="0"
                                    maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>
</xs:element>

It's much easier in RELAX NG, in which you can use interleave as
follows:

  <element name="element">
    <interleave>
      <element name="aa" />
      <element name="bb" />
      <zeroOrMore>
        <element name="cc" />
      </zeroOrMore>
      <zeroOrMore>
        <element name="dd" />
      </zeroOrMore>
    </interleave>
  </element>

Cheers,

Jeni

---
Jeni Tennison
http://www.jenitennison.com/

Received on Friday, 23 July 2004 08:33:32 UTC