1

Let say we have the following schema (from a Microsoft sample):

<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>

The sequence is optional, so all elements below can appear or not.

Now, if we have:

<xs:element name="zooAnimals">
  <xs:complexType>
        <xs:sequence minOccurs="0" maxOccurs="unbounded">
           <xs:element name="elephant" minOccurs="0" maxOccurs="1"/>
           <xs:element name="bear" minOccurs="1" maxOccurs="unbounded"/>
           <xs:element name="giraffe" minOccurs="1" maxOccurs="1"/>
        </xs:sequence>
  </xs:complexType>
</xs:element>

Elements bear and giraffe must be present if zooAnimals is present.
Up to now, I'm OK.

But what if we have this (mix of the above example and "real life" XSD)?

<xs:element name="zooAnimals">
  <xs:complexType>
        <xs:sequence minOccurs="1" maxOccurs="1">
           <xs:element name="elephant" minOccurs="1" maxOccurs="1"/>
           <xs:element name="bear" minOccurs="0" maxOccurs="1"/>
           <xs:element name="giraffe" minOccurs="0" maxOccurs="1"/>
        </xs:sequence>
  </xs:complexType>
</xs:element>  

If the sequence is mandatory, why specify minOccurs in elements, and why some ones can be with minOccurs="0"?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Jean-Philippe
  • 173
  • 10

2 Answers2

2

elements bear and giraffe must be present if zooAnimals is present. Up to now, I'm OK

This may be where you might have to adjust your understanding.

Because the xs:sequence is optional, zooAmimals may still be present without any of elephant, bear, or giraffe being present.

If the sequence is mandatory, why specify minOccurs in elements, and why some ones can be with minOccurs="0"?

You're missing that there are two levels of occurrence constraints in play:

  1. On xs:sequence, the occurrence constraints apply to the sequence group collectively. The collective group might be optional, required, or be able to be repeated as a group.

  2. On the xs:element children of xs:sequence, the occurrence constraints apply to the children individually after taking into account the occurrence constraints on the xs:sequence as a group. If the xs:sequence is, say, optional, and the group is omitted, the individual xs:element constraints do not matter; otherwise, the xs:element occurrence constraints govern the occurrences of each element individually within each xs:sequence group occurrence.

See also

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • kjhughes: when you say "Because the xs:sequence is optional, zooAmimals may still be present without any...", does it mean that the sequence may be present and empty? – Jean-Philippe Oct 13 '22 at 15:01
  • Close: `xs:sequence/minOccurs="0"` means that that the sequence **as a group** is optional, therefore all of the subordinate elements may be omitted **as a group**, regardless of their individual occurrence constraints. – kjhughes Oct 13 '22 at 15:40
  • 1
    Ok, it's clear now. Thank you for your explanations – Jean-Philippe Oct 14 '22 at 07:49
2

In your first example

   <xs:sequence minOccurs="0" maxOccurs="unbounded">
       <xs:element name="elephant"/>
       <xs:element name="bear"/>
       <xs:element name="giraffe"/>
   </xs:sequence>

your elements must appear in groups of three: elephant, bear, giraffe, elephant, bear, giraffe, elephant, bear, giraffe, ...

In your second example,

<xs:sequence minOccurs="0" maxOccurs="unbounded">
   <xs:element name="elephant" minOccurs="0" maxOccurs="1"/>
   <xs:element name="bear" minOccurs="1" maxOccurs="unbounded"/>
   <xs:element name="giraffe" minOccurs="1" maxOccurs="1"/>
</xs:sequence>

Each group consists of zero elephants, any number of bears, and a giraffe. So you might have E B B B B G E B G B B G. But you can't have E B G G because a G ends the group and the next group must start with E or B.

(This is reminding me of Gödel, Escher, Bach....)

In your third example

<xs:sequence minOccurs="1" maxOccurs="1">
   <xs:element name="elephant" minOccurs="1" maxOccurs="1"/>
   <xs:element name="bear" minOccurs="0" maxOccurs="1"/>
   <xs:element name="giraffe" minOccurs="0" maxOccurs="1"/>
</xs:sequence>

The sequence can't repeat. You can have E B G or E or E G or E B, but you can only have one such group, because the sequence itself can only occur once.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • [*smile*] XSD being XML, the XSD schema schema, XSLT being XML, and XSLT that generates XSLT all remind me fondly of Gödel, Escher, Bach. – kjhughes Oct 13 '22 at 15:54
  • Michaels, your explanations, together with those of KjHughes, make things very clear now. Thank you both – Jean-Philippe Oct 17 '22 at 07:33