ZVON > Tutorials > XML Schema and Relax NG Tutorial
Index | >> Example 2 / 8 << | Prev | Next |
Contents > Restrictions > Restriction of a choice

Restriction of a choice

  1. XML Schema
XML Schema keys: choice, restriction

1. XML Schema

The Schema declares type "AAA", which can contain up to two choices from "x" or "y" elements. Then we declare the type "BBB", which is a restriction of the type "AAA" and contain only one x-y choice. Schema Component Constraint: Particle Derivation OK (Choice:Choice -- RecurseLax) .

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <x>1</x>
</root>

Valid document


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <y>2</y>
</root>

Invalid document
The document is not valid, because the type BBB allows the choice from "x" or "y" to occur only once.


<root xsi:noNamespaceSchemaLocation="correct_0.xsd" xmlns="" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
  <x>1</x>
  <y>2</y>
</root>

Correct XML Schema (correct_0.xsd)


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" >

  <xsd:element name="root" type="BBB"/>

  <xsd:complexType name="AAA">
    <xsd:choice maxOccurs="2">
      <xsd:element name="x" type="xsd:string" minOccurs="1" maxOccurs="1"/>
      <xsd:element name="y" type="xsd:string" minOccurs="1" maxOccurs="1"/>
    </xsd:choice>
  </xsd:complexType>

  <xsd:complexType name="BBB">
    <xsd:complexContent>
      <xsd:restriction base="AAA">
        <xsd:choice maxOccurs="1">
          <xsd:element name="x" type="xsd:string" minOccurs="1" maxOccurs="1"/>
          <xsd:element name="y" type="xsd:string" minOccurs="1" maxOccurs="1"/>
        </xsd:choice>
      </xsd:restriction>
    </xsd:complexContent>
  </xsd:complexType>
</xsd:schema>