Re: using the maxOccurs contruct with sequences.

> In the following example, there are two representations of an unbounded 
> sequence of elements in  "ReportOptionsModel".   Is the first 
> representation ever justified?  If it is, when should it be used?

Hi Ken,

With a single element in the sequence (as you have in each example) it 
is really up to you or the preference of the users of your XML Schema. 
When working with multiple elements in the sequence though, the 
difference is important:

         <xs:complexType name="ReportOptionsModel">
           <xs:sequence maxOccurs="unbounded">
             <xs:element name="FilterA"/>
             <xs:element name="FilterB"/>
           </xs:sequence>
         </xs:complexType>

         <xs:complexType name="ReportOptionsModel2">
           <xs:sequence>
             <xs:element name="FilterA" maxOccurs="unbounded"/>
             <xs:element name="FilterB" maxOccurs="unbounded"/>
           </xs:sequence>
         </xs:complexType>

In the first case, the instance might have:

   <FilterA/>
   <FilterB/>
   <FilterA/>
   <FilterB/>
   <FilterA/>
   <FilterB/>
   ...

In the second case it would be:

   <FilterA/>
   <FilterA/>
   <FilterA/>
   <FilterA/>
   <FilterB/>
   <FilterB/>
   ...

Where all of the FilterA elements would appear before all of the FilterB 
elements.

In XML Schemas where you have some sequences with only a single element 
another consideration is how the document will be modified in subsequent 
versions. If it is your anticipation that more elements will be added to 
the sequence at a later date (and you are indifferent to the order shown 
above) then I would place it on the containing <xs:sequence>. In all 
other cases I would place the cardinality constraint on the 
<xs:element>. That is just my taste though, I prefer to have the 
cardinality constraint as close to the thing it constrains as possible.

All the best,
Jeff Rafter

Received on Monday, 14 March 2005 17:10:43 UTC