JavierTP
JavierTP

Reputation: 35

Can XSD choice be empty?

I've read the XSD specification but I'm still confused. Is an empty choice valid in XSD? I mean, with a simple choice with no attributes declared, is mandatory to have either element "a" or element "b", or can it be empty?

<xs:complexType name="ChoiceType">
    <xs:choice>
        <xs:element name="a" type="type_a"/>
        <xs:element name="b" type="type_b"/>
    </xs:choice>
</xs:complexType>

Upvotes: 3

Views: 2305

Answers (3)

Michael Kay
Michael Kay

Reputation: 163262

Actually it's quite legal to say

<xs:choice minOccurs="17"/>

but there is no instance document that will be valid against this content model. The instance must conform to one of the alternatives, and if there are no alternatives, then this isn't going to be possible.

It can actually be useful to define types that no instance will satisfy, e.g. if you want to restrict a type down to nothing.

Upvotes: 1

kjhughes
kjhughes

Reputation: 111491

Yes, the element governed by an xsd:choice can be empty, if minOccurs="0" is used:

  • minOccurs="0" can be on the xsd:choice itself.
  • minOccurs="0" can be on all of the children.

Related answer: An xsd:choice itself can be empty and would indicate that the element being defined must be empty.

Finally, note that in your example, no, one of a or b must be present because the default for minOccurs is 1.

Upvotes: 2

Xstian
Xstian

Reputation: 8272

Could be empty in this scenario

<xs:complexType name="ChoiceType">
    <xs:choice minOccurs="0">
        <xs:element name="a" type="type_a"/>
        <xs:element name="b" type="type_b"/>
    </xs:choice>
</xs:complexType>

anyway see this question

Upvotes: 1

Related Questions