XML Schema choice element

Definition and Usage

The XML Schema choice element allows only one of the elements included in the <choice> declaration to appear in the containing element.

Element Information

Occurrence Once in group and complexType elements; unlimited otherwise.
Parent Element group, choice, sequence, complexType, restriction (simpleContent), extension (simpleContent), restriction (complexContent), extension (complexContent)
Content annotation, any, choice, element, group, sequence

Syntax

<choice
id=ID
maxOccurs=nonNegativeInteger|unbounded
minOccurs=nonNegativeInteger
any attributes
>
(annotation?,(element|group|choice|sequence|any)*)
</choice>

(The ? symbol declares that the element can appear zero or one times, and the * symbol declares that the element can appear zero or more times.)

Attribute Description
id Optional. Specifies a unique ID for the element.
maxOccurs Optional. Specifies the maximum number of times the choice element can appear in the parent element. This value can be any non-negative integer. To set no limit on the maximum number, use the string "unbounded". The default value is 1.
minOccurs Optional. Specifies the minimum number of times the choice element can appear in the parent element. This value can be any non-negative integer. To specify that this any group is optional, set this attribute to zero. The default value is 1.
any attributes Optional. Specifies any other attributes with a non-schema namespace.

Example

<xs:element name="person">
  <xs:complexType>
    <xs:choice>
      <xs:element name="employee" type="employee"/>
      <xs:element name="member" type="member"/>
    </xs:choice>
  </xs:complexType>
</xs:element>

The example above defines that the "person" element must contain either an "employee" element or a "member" element.