Reputation: 285
I'm trying to understand how xsd:choice and minOccurs work together. I read this topic, but it just responded to half of my question. For example, I've this type :
<xsd:element name="CAR">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:element name="A"/>
<xsd:element name="B"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
If I want to make it possible to not specify any of CAR
's elements (neither A nor B), I can add minOccurs like this:
<xsd:element name="CAR">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:element name="A" minOccurs="0"/>
<xsd:element name="B" minOccurs="0"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
But is it true that if I put it on only one element, either A or B, it will result on the same? In other words, is it true that my previous example =
<xsd:element name="CAR">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:element name="A" minOccurs="0"/>
<xsd:element name="B"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
=
<xsd:element name="CAR">
<xsd:complexType>
<xsd:sequence>
<xsd:choice>
<xsd:element name="A"/>
<xsd:element name="B" minOccurs="0"/>
</xsd:choice>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
In these three examples, I'll be able to specify any element (neither A nor B), right?
Upvotes: 4
Views: 3660
Reputation: 338208
I'd express it a little differently:
<xsd:element name="CAR">
<xsd:complexType>
<xsd:choice minOccurs="0">
<xsd:element name="A" />
<xsd:element name="B" />
</xsd:choice>
</xsd:complexType>
</xsd:element>
But to answer your question, a choice between <A>
or <B>
where both are optional (minOccurs="0"
) is logically equivalent to a choice between <A>
or <B>
where either one is optional.
In other words, one of the conditions of the <xsd:choice>
must be fulfilled. By declaring minOccurs="0"
on one of the options, you effectively allow that to match when no child is found (i.e. "There is no <B>
, so what we have here must be an <A>
which is missing, which is OK.")
Upvotes: 5