Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 16: Substitution groups

Full example

This example illustrates substitution groups. The element declarations for shirt, hat and umbrella are all in the substitution group whose head is product.

Instance (chapter16.xml)
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter16.xsd">
  <product>
    <number>999</number>
    <name>Special Seasonal</name>
  </product>
  <shirt>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <color value="blue"/>
  </shirt>
  <hat>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <size>L</size>
  </hat>
  <umbrella>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
  </umbrella>
</items>
Schema (chapter16.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="items" type="ItemsType"/>
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element ref="product" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="shirt" type="ShirtType"
                substitutionGroup="product"/>
  <xs:complexType name="ShirtType">
    <xs:complexContent>
      <xs:extension base="ProductType">
        <xs:sequence>
          <xs:element name="size" type="ShirtSizeType"/>
          <xs:element name="color" type="ColorType"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="hat" substitutionGroup="product">
  <xs:complexType>
    <xs:complexContent>
      <xs:extension base="ProductType">
        <xs:sequence>
          <xs:element name="size" type="HatSizeType"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  </xs:element>
  <xs:element name="umbrella" substitutionGroup="product"/>
  <xs:complexType name="ShirtSizeType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="system" type="xs:token"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="HatSizeType">
    <xs:simpleContent>
      <xs:extension base="xs:token">
        <xs:attribute name="system" type="xs:token"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="ColorType">
    <xs:attribute name="value" type="xs:string"/>
  </xs:complexType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 16-1. The head of a substitution group
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="items" type="ItemsType"/>
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element ref="product" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 16-2. Members of a substitution group
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="shirt" type="ShirtType"
              substitutionGroup="product"/>
  <xs:complexType name="ShirtType">
    <xs:complexContent>
      <xs:extension base="ProductType">
        <xs:sequence>
          <xs:element name="size" type="ShirtSizeType"/>
          <xs:element name="color" type="ColorType"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:element name="hat" substitutionGroup="product">
    <xs:complexType>
      <xs:complexContent>
        <xs:extension base="ProductType">
          <xs:sequence>
            <xs:element name="size" type="HatSizeType"/>
          </xs:sequence>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:element>
  <xs:element name="umbrella" substitutionGroup="product"/>
  <!--...-->
</xs:schema>
Example 16-3. Instance of items
<items>
  <product>
    <number>999</number>
    <name>Special Seasonal</name>
  </product>
  <shirt>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <color value="blue"/>
  </shirt>
  <hat>
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <size>L</size>
  </hat>
  <umbrella>
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
  </umbrella>
</items>
Example 16-4. Substitution group with simple types
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="number"/>
  <xs:element name="skuNumber" type="xs:string"
              substitutionGroup="number"/>
  <xs:element name="productID" type="xs:integer"
              substitutionGroup="number"/>
</xs:schema>
Example 16-5. A member of two substitution groups
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="product" type="ProductType"/>
  <xs:element name="discontinuedProduct" type="ProductType"/>
  <xs:element name="hat" type="HatType"
              substitutionGroup="product"/>
  <xs:element name="shirt" type="ShirtType"
              substitutionGroup="product"/>
  <xs:element name="umbrella" type="UmbrellaType"
              substitutionGroup="product discontinuedProduct"/>
  <!--...-->
</xs:schema>
Example 16-6. Using a choice group
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="items" type="ItemsType"/>
  <xs:complexType name="ItemsType">
    <xs:group ref="ProductGroup" maxOccurs="unbounded"/>
  </xs:complexType>
  <xs:group name="ProductGroup">
    <xs:choice>
      <xs:element name="product" type="ProductType"/>
      <xs:element name="shirt" type="ShirtType"/>
      <xs:element name="hat" type="HatType"/>
      <xs:element name="umbrella" type="ProductType"/>
    </xs:choice>
  </xs:group>
  <!--...-->
</xs:schema>
Example 16-7. Defining derived types
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ShirtType">
    <xs:complexContent>
      <xs:extension base="ProductType">
        <xs:sequence>
          <xs:element name="size" type="ShirtSizeType"/>
          <xs:element name="color" type="ColorType"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="HatType">
    <xs:complexContent>
      <xs:extension base="ProductType">
        <xs:sequence>
          <xs:element name="size" type="HatSizeType"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="UmbrellaType">
    <xs:complexContent>
      <xs:extension base="ProductType"/>
    </xs:complexContent>
  </xs:complexType>
  <!--...-->
</xs:schema>
Example 16-8. Valid instance using derived types
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <product>
    <number>999</number>
    <name>Special Seasonal</name>
  </product>
  <product xsi:type="ShirtType">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <color value="blue"/>
  </product>
  <product xsi:type="HatType">
    <number>563</number>
    <name>Ten-Gallon Hat</name>
    <size>L</size>
  </product>
  <product xsi:type="UmbrellaType">
    <number>443</number>
    <name>Deluxe Golf Umbrella</name>
  </product>
</items>
Example 16-9. Using final to control substitution group declaration
<xs:element name="product" type="ProductType" final="#all"/>
<xs:element name="items" type="ItemsType" final="extension"/>
<xs:element name="color" type="ColorType" final="restriction"/>
<xs:element name="size" type="SizeType" final=""/>
Example 16-10. Using block to prevent substitution group use
<xs:element name="product" type="ProductType" block="#all"/>
<xs:element name="hat" type="HatType" block="substitution"/>
Example 16-11. An abstract element declaration
<xs:element name="product" type="ProductType" abstract="true"/>
Datypic XML Schema Services