Creating a Set of Choices : choice « XML Schema « XML Tutorial






With the default minOccurs and maxOccurs attribute values (both equal to 1), only one of the elements in a set of choices can appear. 
If the value of the maxOccurs attribute is greater than 1, that value determines how many of the choices may appear. 
A set of choices can also contain nested sequences, additional choice sets, or references to named groups.
A set of choices may be contained in a complex type definition, in sequences, in other sets of choices, or in named group definitions.

 
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.java2s.com" xmlns="http://www.java2s.com"
  elementFormDefault="qualified">

    <xsd:element name="animal" type="animalType" />

  <xsd:complexType name="animalType">
    <xsd:choice>
      <xsd:element name="subspecies" type="xsd:string" />
      <xsd:sequence>
        <xsd:element name="region" type="xsd:string" />
        <xsd:element name="population" type="xsd:integer" />
      </xsd:sequence>
    </xsd:choice>
  </xsd:complexType>
</xsd:schema>
 

File: Data.xml
<?xml version="1.0"?>
<animal xmlns="http://www.java2s.com">
  <subspecies>Russia</subspecies>
</animal>








3.58.choice
3.58.1.Creating a Set of Choices
3.58.2. Declarations
3.58.3.Allow the document instance author to use the initial element twice
3.58.4.You can further express occurrence behavior by using the minOccurs and maxOccurs attributes with either the choice element or the element declarations themselves.