XML Tutorial/XML Schema/choice
Содержание
Allow the document instance author to use the initial element twice
<source lang="xml">
<xsd:element name="middleName">
<xsd:complexType> <xsd:choice> <xsd:element name="initial" type="xsd:string" maxOccurs="2"/> <xsd:element name="name" type="xsd:string"/> </xsd:choice> </xsd:complexType>
</xsd:element></source>
<choice> Declarations
<source lang="xml">
<choice minOccurs="non negative number"
maxOccurs="non negative number or unbounded">
You can specify multiple child declarations within a <choice> declaration. In an instance document, only one of the declarations may be used. <choice>
<element name="first" type="string" minOccurs="1" maxOccurs="unbounded"/> <element name="middle" type="string" minOccurs="0" maxOccurs="1"/> <element name="last" type="string"/>
</choice> <choice> declaration may contain <element> declarations, element wild-cards, and inner <sequence>, <choice>, or <group>references.</source>
Creating a Set of Choices
<source lang="xml">
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.wbex.ru" xmlns="http://www.wbex.ru" 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.wbex.ru">
<subspecies>Russia</subspecies>
</animal></source>
You can further express occurrence behavior by using the minOccurs and maxOccurs attributes with either the choice element or the element declarations themselves.
<source lang="xml">
<xsd:element name="middleName">
<xsd:complexType> <xsd:choice maxOccurs="2"> <xsd:element name="initial" type="xsd:string"/> <xsd:element name="name" type="xsd:string"/> </xsd:choice> </xsd:complexType>
</xsd:element> The middleName element to contain one of two child elements: initial or name <middleName>
<initial>S</initial>
</middleName> <middleName>
<name>Stolte</name>
</middleName> To allow for this content model, we would have to use the choice element: <xsd:element name="middleName">
<xsd:complexType> <xsd:choice> <xsd:element name="initial" type="xsd:string"/> <xsd:element name="name" type="xsd:string"/> </xsd:choice> </xsd:complexType>
</xsd:element></source>