Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should XSD occurrence bounds be on the sequence or the element?

Tags:

xml

xsd

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>
like image 731
sk. Avatar asked Feb 11 '09 21:02

sk.


People also ask

What is the use of sequence in XSD?

Definition and Usage 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.

What is the default minOccurs in XSD?

The default value for both the minOccurs and the maxOccurs attributes is 1. Thus, when an element such as comment is declared without a maxOccurs attribute, the element may not occur more than once.

What is XS all in XSD?

<xsd:all> ElementAllows the elements in the group to appear (or not appear) in any order in the containing element. Copy. <all id = ID maxOccurs= 1: 1 minOccurs= (0 | 1): 1 {any attributes with non-schema Namespace...


1 Answers

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.

like image 88
Eddie Avatar answered Oct 14 '22 04:10

Eddie