Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 14: Deriving complex types

Full example

This example illustrates complex types that are derived from other specified types.

Instance (chapter14.xml)
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter14.xsd">
  <hat routingNum="123456" effDate="2002-04-02" lang="en-US">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
    <description>This is a great hat!</description>
  </hat>
  <umbrella routingNum="123" effDate="2002-04-02">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
  </umbrella>
  <shirt routingNum="124" effDate="2002-04-02" sleeve="17">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <color value="blue"/>
    <size system="US-DRESS">6</size>
  </shirt>
</items>
Schema (chapter14.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="items" type="ItemsType"/>
  <xs:complexType name="ItemsType">
    <xs:choice minOccurs="0" maxOccurs="unbounded">
      <xs:element name="hat" type="ProductType"/>
      <xs:element name="umbrella" type="RestrictedProductType"/>
      <xs:element name="shirt" type="ShirtType"/>
    </xs:choice>
  </xs:complexType>
  <!--Empty Content Type-->
  <xs:complexType name="ItemType" abstract="true">
    <xs:attribute name="routingNum" type="xs:integer"/>
  </xs:complexType>
  <!--Empty Content Extension (with Attribute Extension)-->
  <xs:complexType name="ProductType">
    <xs:complexContent>
      <xs:extension base="ItemType">
        <xs:sequence>
          <xs:element name="number" type="xs:integer"/>
          <xs:element name="name" type="xs:string"/>
          <xs:element name="description"
                       type="xs:string" minOccurs="0"/>
        </xs:sequence>
        <xs:attribute name="effDate" type="xs:date"/>
        <xs:attribute name="lang" type="xs:language"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <!--Complex Content Restriction-->
  <xs:complexType name="RestrictedProductType">
    <xs:complexContent>
      <xs:restriction base="ProductType">
        <xs:sequence>
          <xs:element name="number" type="xs:integer"/>
          <xs:element name="name" type="xs:token"/>
        </xs:sequence>
        <xs:attribute name="routingNum"
                       type="xs:short" use="required"/>
        <xs:attribute name="effDate"
                       type="xs:date" default="1900-01-01"/>
        <xs:attribute name="lang" use="prohibited"/>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
  <!--Complex Content Extension-->
  <xs:complexType name="ShirtType">
    <xs:complexContent>
      <xs:extension base="RestrictedProductType">
        <xs:choice maxOccurs="unbounded">
          <xs:element name="size" type="SmallSizeType"/>
          <xs:element name="color" type="ColorType"/>
        </xs:choice>
        <xs:attribute name="sleeve" type="xs:integer"/>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <!--Simple Content Extension-->
  <xs:complexType name="SizeType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="system" type="xs:token"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <!--Simple Content Restriction-->
  <xs:complexType name="SmallSizeType">
    <xs:simpleContent>
      <xs:restriction base="SizeType">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="6"/>
        <xs:attribute  name="system" type="xs:token"
                        use="required"/>
      </xs:restriction>
    </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 14-1. Simple content extension
Schema:
<xsd:complexType name="SizeType">
  <xsd:simpleContent>
    <xsd:extension base="xsd:integer">
      <xsd:attribute name="system" type="xsd:token"/>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>
Instance:
<size system="US-DRESS">10</size>
Example 14-2. Complex content extension
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ShirtType">
  <xsd:complexContent>
    <xsd:extension base="ProductType">
      <xsd:choice maxOccurs="unbounded">
        <xsd:element name="size" type="SizeType"/>
        <xsd:element name="color" type="ColorType"/>
      </xsd:choice>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 14-3. Effective content model of ShirtType
<xsd:complexType name="ShirtType">
  <xsd:sequence>
    <xsd:sequence>
      <xsd:element name="number" type="ProdNumType"/>
      <xsd:element name="name" type="xsd:string"/>
    </xsd:sequence>
    <xsd:choice maxOccurs="unbounded">
      <xsd:element name="size" type="SizeType"/>
      <xsd:element name="color" type="ColorType"/>
    </xsd:choice>
  </xsd:sequence>
</xsd:complexType>
Example 14-4. choice group extension
<xsd:complexType name="ItemsType">
  <xsd:choice maxOccurs="unbounded">
    <xsd:element ref="shirt"/>
    <xsd:element ref="hat"/>
    <xsd:element ref="umbrella"/>
  </xsd:choice>
</xsd:complexType>
<xsd:complexType name="ExpandedItemsType">
  <xsd:complexContent>
    <xsd:extension base="ItemsType">
      <xsd:choice maxOccurs="unbounded">
        <xsd:element ref="sweater"/>
        <xsd:element ref="suit"/>
      </xsd:choice>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 14-5. Mixed content extension
<xsd:complexType name="LetterType" mixed="true">
  <xsd:sequence>
    <xsd:element name="custName" type="PersonNameType"/>
    <xsd:element name="prodName" type="xsd:string"/>
    <xsd:element name="prodSize" type="SizeType"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="ExtendedLetterType" mixed="true">
  <xsd:complexContent>
    <xsd:extension base="LetterType">
      <xsd:sequence>
        <xsd:element name="prodNum" type="ProdNumType"/>
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 14-6. Empty content extension
<xsd:complexType name="ItemType">
  <xsd:attribute name="routingNum" type="xsd:integer"/>
</xsd:complexType>
<xsd:complexType name="ProductType">
  <xsd:complexContent>
    <xsd:extension base="ItemType">
      <xsd:sequence>
        <xsd:element name="number" type="ProdNumType"/>
        <xsd:element name="name" type="xsd:string"/>
      </xsd:sequence>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 14-7. Attribute extension
Schema:
<xsd:complexType name="ItemType">
  <xsd:attribute name="id" type="xsd:ID" use="required"/>
  <xsd:attribute ref="xml:lang"/>
</xsd:complexType>
<xsd:complexType name="ProductType">
  <xsd:complexContent>
    <xsd:extension base="ItemType">
      <xsd:attribute name="effDate" type="xsd:date"/>
      <xsd:attribute name="lang" type="xsd:language"/>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Instance:
<product id="prod557"
  xml:lang="en"
  lang="en"
  effDate="2001-04-02"/>
Example 14-8. Attribute wildcard extension
<xsd:complexType name="BaseType">
  <xsd:anyAttribute processContents="lax"
                    namespace="##local
                               http://example.org/prod"/>
</xsd:complexType>
<xsd:complexType name="DerivedType">
  <xsd:complexContent>
    <xsd:extension base="BaseType">
      <xsd:anyAttribute processContents="strict"
                        namespace="##targetNamespace
                                   http://www.w3.org/1999/xhtml"/>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 14-9. Effective attribute wildcard
<xsd:complexType name="DerivedType">
    <xsd:anyAttribute processContents="strict"
                      namespace="##local
                                 http://example.org/prod
                                 ##targetNamespace
                                 http://www.w3.org/1999/xhtml"/>
</xsd:complexType>
Example 14-10. Simple content restriction
<xsd:complexType name="SmallSizeType">
  <xsd:simpleContent>
    <xsd:restriction base="SizeType">
      <xsd:minInclusive value="2"/>
      <xsd:maxInclusive value="6"/>
      <xsd:attribute name="system" type="xsd:token"
                     use="required"/>
    </xsd:restriction>
  </xsd:simpleContent>
</xsd:complexType>
Example 14-11. Complex content restriction
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="size" type="SizeType" minOccurs="0"/>
    <xsd:element name="color" type="ColorType" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RestrictedProductType">
  <xsd:complexContent>
    <xsd:restriction base="ProductType">
      <xsd:sequence>
        <xsd:element name="number" type="ProdNumType"/>
        <xsd:element name="name" type="xsd:string"/>
      </xsd:sequence>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-12. Eliminating meaningless groups
Base group:
<xsd:sequence>
  <xsd:sequence>
    <xsd:element name="a"/>
    <xsd:element name="b"/>
  </xsd:sequence>
</xsd:sequence>
Legal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element name="b"/>
</xsd:sequence>
Example 14-13. Restricting element declarations
Base group:
<xsd:sequence>
  <xsd:element name="a" maxOccurs="3"/>
  <xsd:element name="b" fixed="bValue"/>
  <xsd:element name="c" type="xsd:string"/>
</xsd:sequence>
Legal restriction:
<xsd:sequence>
  <xsd:element name="a" maxOccurs="2"/>
  <xsd:element name="b" fixed="bValue"/>
  <xsd:element name="c" type="xsd:token"/>
</xsd:sequence>
Illegal restriction:
<xsd:sequence>
  <xsd:element name="a" maxOccurs="4"/>
  <xsd:element name="b" fixed="newValue"/>
  <xsd:element name="c" type="xsd:integer"/>
</xsd:sequence>
Example 14-14. Replacing a wildcard with element declarations
Base group:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:any namespace="##other" maxOccurs="1"/>
</xsd:sequence>
Legal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element ref="otherns:b"/>
</xsd:sequence>
Illegal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element ref="b"/>
  <xsd:element name="c"/>
</xsd:sequence>
Example 14-15. Replacing a wildcard with another wildcard
Base wildcard:
<xsd:any namespace="urn:a:1 urn:a:2" maxOccurs="2"/>
Legal restriction:
<xsd:any namespace="urn:a:1" maxOccurs="1"/>
Illegal restriction:
<xsd:any namespace="##other" maxOccurs="3"/>
Example 14-16. Replacing a group with an element declaration
Base group:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element name="b" minOccurs="0"/>
</xsd:sequence>
Legal restriction:
<xsd:element name="a"/>
Example 14-17. Restricting occurrence constraints of a group
Base group:
<xsd:sequence minOccurs="2" maxOccurs="5">
  <xsd:element name="a"/>
  <xsd:element name="b"/>
</xsd:sequence>
Legal restriction:
<xsd:sequence minOccurs="3" maxOccurs="4">
  <xsd:element name="a"/>
  <xsd:element name="b"/>
</xsd:sequence>
Illegal restriction:
<xsd:sequence minOccurs="0" maxOccurs="6">
  <xsd:element name="a"/>
  <xsd:element name="b"/>
</xsd:sequence>
Example 14-18. Maintaining the order of the children in an all group
Base group:
<xsd:all>
  <xsd:element name="a"/>
  <xsd:element name="b" minOccurs="0"/>
  <xsd:element name="c"/>
</xsd:all>
Legal restriction:
<xsd:all>
  <xsd:element name="a"/>
  <xsd:element name="c"/>
</xsd:all>
Illegal restriction:
<xsd:all>
  <xsd:element name="c"/>
  <xsd:element name="a"/>
</xsd:all>
Example 14-19. Restricting an all group
Base group:
<xsd:all>
  <xsd:element name="a"/>
  <xsd:element name="b" minOccurs="0"/>
  <xsd:element name="c"/>
</xsd:all>
Legal restriction:
<xsd:all>
  <xsd:element name="a"/>
  <xsd:element name="c"/>
</xsd:all>
Illegal restriction:
<xsd:all>
  <xsd:element name="a"/>
  <xsd:element name="b"/>
</xsd:all>
Example 14-20. Restricting a choice group
Base group:
<xsd:choice>
  <xsd:element name="a"/>
  <xsd:element name="b"/>
  <xsd:element name="c"/>
</xsd:choice>
Legal restriction:
<xsd:choice>
  <xsd:element name="a"/>
  <xsd:element name="c"/>
</xsd:choice>
Illegal restriction:
<xsd:choice>
  <xsd:element name="a"/>
  <xsd:element name="d"/>
</xsd:choice>
Example 14-21. Replacing an all group with a sequence group
Base group:
<xsd:all>
  <xsd:element name="a"/>
  <xsd:element name="b" minOccurs="0"/>
  <xsd:element name="c"/>
</xsd:all>
Legal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element name="c"/>
</xsd:sequence>
Illegal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element name="b"/>
  <xsd:element name="c" minOccurs="2"/>
</xsd:sequence>
Example 14-22. Replacing a choice group with a sequence group
Base group:
<xsd:choice maxOccurs="2">
  <xsd:element name="a"/>
  <xsd:element name="b"/>
  <xsd:element name="c"/>
</xsd:choice>
Legal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element name="c"/>
</xsd:sequence>
Illegal restriction:
<xsd:sequence>
  <xsd:element name="a"/>
  <xsd:element name="b"/>
  <xsd:element name="c"/>
</xsd:sequence>
Example 14-23. Mixed content restriction
<xsd:complexType name="LetterType" mixed="true">
  <xsd:sequence>
    <xsd:element name="custName" type="PersonNameType"/>
    <xsd:element name="prodName" type="xsd:string"/>
    <xsd:element name="prodSize" type="SizeType" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RestrictedLetterType" mixed="true">
  <xsd:complexContent>
    <xsd:restriction base="LetterType">
      <xsd:sequence>
        <xsd:element name="custName" type="PersonNameType"/>
        <xsd:element name="prodName" type="xsd:string"/>
      </xsd:sequence>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-24. Mixed content restricted to simple content
<xsd:complexType name="LetterType" mixed="true">
  <xsd:sequence minOccurs="0">
    <xsd:element name="custName" type="PersonNameType"/>
    <xsd:element name="prodName" type="xsd:string"/>
    <xsd:element name="prodSize" type="SizeType"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:complexType name="RestrictedLetterType">
  <xsd:simpleContent>
    <xsd:restriction base="LetterType">
      <xsd:simpleType>
        <xsd:restriction base="xsd:string"/>
      </xsd:simpleType>
    </xsd:restriction>
  </xsd:simpleContent>
</xsd:complexType>
Example 14-25. Empty content restriction
<xsd:complexType name="ItemType">
  <xsd:attribute name="routingNum" type="xsd:integer"/>
</xsd:complexType>
<xsd:complexType name="RestrictedItemType">
  <xsd:complexContent>
    <xsd:restriction base="ItemType">
      <xsd:attribute name="routingNum" type="xsd:short"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-26. Legal restrictions of attributes
<xsd:complexType name="BaseType">
      <xsd:attribute name="a" type="xsd:integer"/>
      <xsd:attribute name="b" type="xsd:string"/>
      <xsd:attribute name="c" type="xsd:string" default="c"/>
      <xsd:attribute name="d" type="xsd:string"/>
      <xsd:attribute name="e" type="xsd:string" fixed="e"/>
      <xsd:attribute name="f" type="xsd:string"/>
      <xsd:attribute name="g" type="xsd:string"/>
      <xsd:attribute name="x" type="xsd:string"/>
</xsd:complexType>
<xsd:complexType name="DerivedType">
  <xsd:complexContent>
    <xsd:restriction base="BaseType">
      <xsd:attribute name="a" type="xsd:positiveInteger"/>
      <xsd:attribute name="b" type="xsd:string" default="b"/>
      <xsd:attribute name="c" type="xsd:string" default="c2"/>
      <xsd:attribute name="d" type="xsd:string" fixed="d"/>
      <xsd:attribute name="e" type="xsd:string" fixed="e"/>
      <xsd:attribute name="f" type="xsd:string" use="required"/>
      <xsd:attribute name="g" type="xsd:string" use="prohibited"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-27. Illegal attribute restrictions
<xsd:complexType name="BaseType2">
      <xsd:attribute name="h" type="xsd:integer"/>
      <xsd:attribute name="i" type="xsd:string" fixed="i"/>
      <xsd:attribute name="j" type="xsd:string" fixed="j"/>
      <xsd:attribute name="k" type="xsd:string" use="required"/>
      <xsd:attribute name="l" type="xsd:string" use="required"/>
</xsd:complexType>
<xsd:complexType name="IllegalDerivedType">
  <xsd:complexContent>
    <xsd:restriction base="BaseType2">
      <xsd:attribute name="h" type="xsd:decimal"/>
      <xsd:attribute name="i" type="xsd:string" fixed="i2"/>
      <xsd:attribute name="j" type="xsd:string" default="j"/>
      <xsd:attribute name="k" type="xsd:string"/>
      <xsd:attribute name="l" type="xsd:string" use="prohibited"/>
      <xsd:attribute ref="pref:l"/>
      <xsd:attribute name="m" type="xsd:string"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-28. Restricting an attribute wildcard
<xsd:complexType name="BaseType">
    <xsd:anyAttribute processContents="lax" namespace="##any"/>
</xsd:complexType>
<xsd:complexType name="DerivedType">
  <xsd:complexContent>
    <xsd:restriction base="BaseType">
      <xsd:anyAttribute processContents="strict"
                        namespace="##targetNamespace
                                   http://www.w3.org/1999/xhtml"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-29. Replacing an attribute wildcard by attributes
<xsd:complexType name="BaseType">
  <xsd:anyAttribute processContents="lax" namespace="##any"/>
</xsd:complexType>
<xsd:complexType name="DerivedType">
  <xsd:complexContent>
    <xsd:restriction base="BaseType">
      <xsd:attribute name="id" type="xsd:ID" use="required"/>
      <xsd:attribute name="name" type="xsd:string"/>
    </xsd:restriction>
  </xsd:complexContent>
</xsd:complexType>
Example 14-30. A derived type
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="product" type="ProductType"/>
<xsd:complexType name="ShirtType">
  <xsd:complexContent>
    <xsd:extension base="ProductType">
      <xsd:choice maxOccurs="unbounded">
        <xsd:element name="size" type="SizeType"/>
        <xsd:element name="color" type="ColorType"/>
      </xsd:choice>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 14-31. Substitution of ShirtType for ProductType
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <product xsi:type="ShirtType">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <color value="blue"/>
  </product>
  <!--...-->
</items>
Example 14-32. Preventing derivation
<xsd:complexType name="ProductType" final="#all">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
Example 14-33. Preventing substitution of derived types
<xsd:complexType name="ProductType" block="extension">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="product" type="ProductType"/>
<xsd:complexType name="ShirtType">
  <xsd:complexContent>
    <xsd:extension base="ProductType">
      <xsd:choice maxOccurs="unbounded">
        <xsd:element name="size" type="SizeType"/>
        <xsd:element name="color" type="ColorType"/>
      </xsd:choice>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<xsd:element name="shirt" type="ShirtType"/>
Example 14-34. Illegal substitution of ShirtType
<product xsi:type="ShirtType">
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <color value="blue"/>
</product>
Example 14-35. An abstract type
<xsd:complexType name="ProductType" abstract="true">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="product" type="ProductType"/>
<xsd:complexType name="ShirtType">
  <xsd:complexContent>
    <xsd:extension base="ProductType">
      <xsd:choice maxOccurs="unbounded">
        <xsd:element name="size" type="SizeType"/>
        <xsd:element name="color" type="ColorType"/>
      </xsd:choice>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
<xsd:element name="shirt" type="ShirtType"/>
Example 14-36. Legal instances of product and shirt
<product xsi:type="ShirtType">
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <color value="blue"/>
</product>
<shirt>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <color value="blue"/>
</shirt>
Example 14-37. Illegal uses of the abstract ProductType
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
</product>
<product xsi:type="ProductType">
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
</product>
Datypic XML Schema Services