views:

130

answers:

3

I've been studying up how to write an XML Schema and I'm stumped on XSD ordering indicators like xs:sequence, xs:all, xs:choice. There seem to be only 3 of them and they're required in complex types. But what if I have the XML like the following:

<row>
    <name>John</name>
    <city>LA</city>
    <country>France</country>
</row>

In which the 3 elements inside <row/> can appear in any order i.e. can appear before <city> and <name> etc. like this:

<row>
    <country>France</country>
    <city>LA</city>
    <name>John</name>
</row>

Does this mean I'll have specify a new <xs:sequence> for each sequence of elements. What if I have something like 20 elements with no specific order. Is there some shortcut here? Am I missing something?

UPDATE: I can't use <xs:all> because I might leave some elements. All of them are not required.

+1  A: 

<xs:all> permits items in any order. Just use it instead of <xs:sequence>.

GSerg
All elements are not mandatory.
Daud
Well, your original question was only about order.
GSerg
+1  A: 

<xsl:all> is still your correct answer here I believe. Each element inside an <xs:all> compositor can have a minOccurs attribute of zero. The result is a set of elements which may occur in any order and any element may occur exactly zero or one times. For example, the below requires that row contains one name element, zero or one city elements and zero or one country elements in any order.

<xs:element name="row"> 
    <xs:complexType>
        <xs:all> 
            <xs:element ref="name"/> 
            <xs:element ref="city" minOccurs='0'/> 
            <xs:element ref="country" minOccurs="0"/>
        </xs:all>
    </xs:complexType>

I may have misunderstood your requirement though.

Nic Gibson
Yup this should work for me. Thanks.
Daud
But what if I they can occur more than one times?
Daud
So... you have three elements, all of which can occur zero or more times. Is that right?
Nic Gibson
Yes, 3 or more elements which can occur zero or more times and in no specific order.
Daud
A: 

Found the answer to this question. Instead of using <xs:all> use <xs:choice> with minOccurs="0" and maxOccurs="unbounded". Now the elements are not only optional they can also appear in any order.

<xs:element name="row">
 <xs:complexType>
   <xs:choice minOccurs="0" maxOccurs="unbounded"> 
     <xs:element name="name" /> 
     <xs:element name="city" /> 
     <xs:element name="country" />
   </xs:all>
 </xs:complexType>
</xs:element>
Daud
FWIW I don't think that's the same at all. This schema allows what it looks like - *any* number of child elements, each of which could be either of the three. Is it valid to have a `<row>` contain five `<name>` children and nothing else? This schema allows that. Newt's answer is closer to what it sounds like you want.
Andrzej Doyle