tags:

views:

508

answers:

3

If I have an element with one or more subelements, should the min/maxoccurs attributes be on the xsd:sequence element, the xsd:element, both, or neither?

<xsd:element name="books">
  <xsd:complexType>
    <xsd:sequence minOccurs="1" maxOccurs="unbounded"> <!-- here? -->
      <xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/> <!-- or here? -->
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
A: 

Put the maxOccurs attribute on the <xsd:element> element.

<xsd:sequence>
  <xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/> <!-- here! -->
</xsd:sequence>
baretta
+1  A: 

It should be on the <xsd:element>, IMHO.

The effect of putting it on the sequence is the same, eventually (for this case). But looking at it from a logical perspective, you want to express that it is a sequence of elements, and not a row of sequences containing one element each.

Putting it on both would definitely feel wrong for me, though for this case it would (again) make no practical difference.

Tomalak
+4  A: 

In almost all circumstances, you want to put the min/max Occurs on the element within a sequence and not on the sequence. Using your example:

<xsd:element name="books">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

This is unequivocal. If you have a series of book elements in a row, you can point to exactly which schema item is producing them. However:

<xsd:element name="books">
  <xsd:complexType>
    <xsd:sequence minOccurs="1" maxOccurs="unbounded"/>
      <xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

Here, if you have two "book" elements in a row, do you have two sequences in a row, or one sequence with two book elements? This fails the Unique Particle Attribution requirement.

Finally, if you put the min/max Occurs on the sequence and you later add an additional element:

<xsd:element name="books">
  <xsd:complexType>
    <xsd:sequence minOccurs="1" maxOccurs="unbounded"/>
      <xsd:element ref="book"/>
      <xsd:element ref="ebook"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

then this may allow the following XML, which is probably not what you intend:

<books>
  <book/><ebook/><book/><ebook/><book/><ebook/><book/><ebook/>
</books>

whereas if you have:

<xsd:element name="books">
  <xsd:complexType>
    <xsd:sequence/>
      <xsd:element ref="book" minOccurs="1" maxOccurs="unbounded"/>
      <xsd:element ref="ebook" minOccurs="1" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>

then it is clear and unambiguous what you intend: A sequence of one or more book elements followed by a sequence of one or more ebook elements.

Eddie