XML Tutorial/XML Schema/choice — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:26, 26 мая 2010

Allow the document instance author to use the initial element twice

<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>


<choice> Declarations

<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.


Creating a Set of Choices

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>


You can further express occurrence behavior by using the minOccurs and maxOccurs attributes with either the choice element or the element declarations themselves.

<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>