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"?
This may be where you might have to adjust your understanding.
Because the
xs:sequenceis optional,zooAmimalsmay still be present without any ofelephant,bear, orgiraffebeing present.You're missing that there are two levels of occurrence constraints in play:
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.On the
xs:elementchildren ofxs:sequence, the occurrence constraints apply to the children individually after taking into account the occurrence constraints on thexs:sequenceas a group. If thexs:sequenceis, say, optional, and the group is omitted, the individualxs:elementconstraints do not matter; otherwise, thexs:elementoccurrence constraints govern the occurrences of each element individually within eachxs:sequencegroup occurrence.See also