views:

223

answers:

1

What I want to do is create a sequence element in an XML schema such that the contents must be in order, but may not all be present. For example, the schema would be something like this:

<xs:element name="rods" maxOccurs="1" minOccurs="1">
 <xs:complexType>
  <xs:sequence>
   <xs:element name="green" type="xs:positiveInteger" />
   <xs:element name="white" type="xs:positiveInteger" />
   <xs:element name="blue" type="xs:positiveInteger" />
   <xs:element name="yellow" type="xs:positiveInteger" />
   <xs:element name="red" type="xs:positiveInteger" />
   <xs:element name="tan" type="xs:positiveInteger" />
   <xs:element name="gray" type="xs:positiveInteger" />
   <xs:element name="black" type="xs:positiveInteger" />
  </xs:sequence>
 </xs:complexType>
</xs:element>

and would allow XML like this:

<rods>
 <green>142</green>
 <white>34</white>
 <gray>65</gray>
</rods>

MSDN has this to say about it:

The following example shows an element (zooAnimals) that can have zero or more of the following elements, elephant, bear, giraffe, in the sequence element.

<xs:element name="zooAnimals">
      <xs:complexType>
            <xs:sequence minOccurs="0" maxOccurs="unbounded">
               <xs:element name="elephant"/>
               <xs:element name="bear"/>
               <xs:element name="giraffe"/>
            </xs:sequence>
      </xs:complexType>
</xs:element>

W3Schools suggests the same. However, both visual studio and an online validation service don't like what is suggested.

Here is what I have at the moment:

<xs:element name="rods" maxOccurs="1" minOccurs="1">
 <xs:complexType>
  <xs:sequence minOccurs="0" maxOccurs="1">
   <xs:element name="green" type="xs:positiveInteger" />
   <xs:element name="white" type="xs:positiveInteger" />
   <xs:element name="blue" type="xs:positiveInteger" />
   <xs:element name="yellow" type="xs:positiveInteger" />
   <xs:element name="red" type="xs:positiveInteger" />
   <xs:element name="tan" type="xs:positiveInteger" />
   <xs:element name="gray" type="xs:positiveInteger" />
   <xs:element name="black" type="xs:positiveInteger" />
  </xs:sequence>
 </xs:complexType>
</xs:element>

And here's the XML:

<rods>
 <green>142</green>
 <white>34</white>
 <gray>65</gray>
</rods>

Visual Studio 2005 claims that "gray" is invalid, "blue" expected, so clearly it wants all of the children

Thanks,

Eric

+2  A: 
<xs:element name="rods">
    <xs:complexType>
        <xs:sequence>
                <xs:element minOccurs="0" name="green" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="white" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="blue" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="yellow" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="red" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="tan" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="gray" type="xs:positiveInteger" />
                <xs:element minOccurs="0" name="black" type="xs:positiveInteger" />
        </xs:sequence>
    </xs:complexType>
</xs:element>
13ren
Thanks, that does the job. There no way of avoiding typing lots of minOccurs="0", is there?I wonder why W3Schools and MSDN say that the other code works, when it doesn't?
Eric
@Eric They are both sequences, but differ in the important details that you want. They mean zero or lots of repetitions of the group, but you wanted each individual to be zero or one. Are you familiar with regex? XML Schema is very similar. Their method is like (e g b)*, but what you want is like e? g? b?
13ren
Ah... Must have misinterpreted what MSDN was saying
Eric