Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you make a sequence of optional items which must be in order in an XML schema?

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

like image 218
Eric Avatar asked May 31 '09 08:05

Eric


People also ask

How do I make an XML element optional?

To include this optional field as part of the default XML for a new form, leave the box checked. To exclude it, clear the check box. All of the non-optional elements have a grayed-out check box, meaning you cannot clear the check box.

How do I make an element optional in XSD?

Using <xsd:choice> in an XSD The element in the root schema has to be optional. Add attribute minOccurs="0" on the element to make it optional.

What is sequence in XML schema?

The sequence element specifies that the child elements must appear in a sequence. Each child element can occur from 0 to any number of times.

Do XML elements have to be in order?

In the most general sense, XML element order does not matter, unless otherwise specified by the appropriate schema.


1 Answers

<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>
like image 57
13ren Avatar answered Sep 28 '22 01:09

13ren