Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st 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
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="items" type="ItemsType"/>
  <xsd:complexType name="ItemsType">
    <xsd:sequence>
      <xsd:element ref="product" maxOccurs="unbounded"/>
    </xsd:sequence>
  </xsd:complexType>
  <xsd:element name="product" type="ProductType"/>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element ref="number"/>
      <xsd:element ref="name"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Example 16-2. Members of a substitution group
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="shirt" type="ShirtType"
          substitutionGroup="product"/>
  <xsd:complexType name="ShirtType">
    <xsd:complexContent>
      <xsd:extension base="ProductType">
        <xsd:sequence>
          <xsd:element name="size" type="ShirtSizeType"/>
          <xsd:element name="color" type="ColorType"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:element name="hat" substitutionGroup="product">
    <xsd:complexType>
      <xsd:complexContent>
        <xsd:extension base="ProductType">
          <xsd:sequence>
            <xsd:element name="size" type="HatSizeType"/>
          </xsd:sequence>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:element>
  <xsd:element name="umbrella" substitutionGroup="product"/>
  <!--...-->
</xsd: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
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="number"/>
  <xsd:element name="skuNumber" type="SKUType"
               substitutionGroup="number"/>
  <xsd:element name="productID" type="ProductIDType"
               substitutionGroup="number"/>
</xsd:schema>
Example 16-5. Using a choice group
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="items" type="ItemsType"/>
  <xsd:complexType name="ItemsType">
    <xsd:group ref="ProductGroup" maxOccurs="unbounded"/>
  </xsd:complexType>
  <xsd:group name="ProductGroup">
    <xsd:choice>
      <xsd:element name="product" type="ProductType"/>
      <xsd:element name="shirt" type="ShirtType"/>
      <xsd:element name="hat" type="HatType"/>
      <xsd:element name="umbrella" type="ProductType"/>
    </xsd:choice>
  </xsd:group>
  <!--...-->
</xsd:schema>
Example 16-6. Defining derived types
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="ShirtType">
    <xsd:complexContent>
      <xsd:extension base="ProductType">
        <xsd:sequence>
          <xsd:element name="size" type="ShirtSizeType"/>
          <xsd:element name="color" type="ColorType"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="HatType">
    <xsd:complexContent>
      <xsd:extension base="ProductType">
        <xsd:sequence>
          <xsd:element name="size" type="HatSizeType"/>
        </xsd:sequence>
      </xsd:extension>
    </xsd:complexContent>
  </xsd:complexType>
  <xsd:complexType name="UmbrellaType">
    <xsd:complexContent>
      <xsd:extension base="ProductType"/>
    </xsd:complexContent>
  </xsd:complexType>
  <!--...-->
</xsd:schema>
Example 16-7. 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-8. Using final to control substitution group declaration
<xsd:element name="product" type="ProductType" final="#all"/>
<xsd:element name="items" type="ItemsType" final="extension"/>
<xsd:element name="color" type="ColorType" final="restriction"/>
<xsd:element name="size" type="SizeType" final=""/>
Example 16-9. Using block to prevent substitution group use
<xsd:element name="product" type="ProductType" block="#all"/>
<xsd:element name="hat" type="HatType" block="substitution"/>
Example 16-10. An abstract element type
<xsd:element name="product" type="ProductType" abstract="true"/>
Datypic XML Schema Services