Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 13: Complex types

Full example

This example illustrates complex types that are not derived from other specified types (they are by default derived from anyType). It includes an example of each content type: element-only, simple, empty and mixed.

Instance (chapter13.xml)
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter13.xsd"
       xmlns:xhtml="http://www.w3.org/1999/xhtml"
       xmlns:oth="http://example.org/oth">
  <shirt effDate="2002-04-02">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size system="US-DRESS">10</size>
    <color value="blue"/>
    <description>
      This shirt is the <xhtml:b>best-selling</xhtml:b> shirt in
      our catalog! <xhtml:br/> Note: runs large.
    </description>
  </shirt>
  <hat oth:custom="12">
    <number>557</number>
    <name>Ten-Gallon Hat</name>
    <color value="red"/>
  </hat>
</items>
Schema (chapter13.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="shirt" type="ProductType"/>
      <xs:element name="hat" type="ProductType"/>
      <xs:element name="umbrella" type="ProductType"/>
    </xs:choice>
  </xs:complexType>
  <!-- Element only content -->
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string"/>
      <xs:choice minOccurs="0" maxOccurs="unbounded">
        <xs:element name="size" type="SizeType"/>
        <xs:element name="color" type="ColorType"/>
        <xs:element name="description" type="DescriptionType"/>
      </xs:choice>
    </xs:sequence>
    <xs:attribute  name="effDate" type="xs:date"
                    default="1900-01-01"/>
    <xs:anyAttribute namespace="##other" processContents="lax"/>
  </xs:complexType>
  <!-- Simple content -->
  <xs:complexType name="SizeType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="system" type="xs:token"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <!-- Empty content -->
  <xs:complexType name="ColorType">
    <xs:attribute name="value" type="xs:string"/>
  </xs:complexType>
  <!-- Mixed content -->
  <xs:complexType name="DescriptionType" mixed="true">
    <xs:sequence>
      <xs:any namespace="http://www.w3.org/1999/xhtml"
      minOccurs="0" maxOccurs="unbounded"
      processContents="skip"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 13-1. Elements with complex types
<size system="US-DRESS">10</size>
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
</product>
<letter>Dear <custName>Priscilla Walmsley</custName>...</letter>
<color value="blue"/>
Example 13-2. Named complex type
<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"/>
  </xsd:sequence>
</xsd:complexType>
<xsd:element name="product" type="ProductType"/>
Example 13-3. Anonymous complex type
<xsd:element name="product">
  <xsd:complexType>
    <xsd:sequence>
      <xsd:element name="number" type="ProdNumType"/>
      <xsd:element name="name" type="xsd:string"/>
      <xsd:element name="size" type="SizeType"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:element>
Example 13-4. Instance element with simple content
<size system="US-DRESS">10</size>
Example 13-5. Complex type with simple content
<xsd:complexType name="SizeType">
  <xsd:simpleContent>
    <xsd:extension base="xsd:integer">
      <xsd:attribute name="system" type="xsd:token"/>
    </xsd:extension>
  </xsd:simpleContent>
</xsd:complexType>
Example 13-6. Instance element with element-only content
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">10</size>
  <color value="blue"/>
</product>
Example 13-7. Complex type with element-only content
<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"/>
    <xsd:element name="color" type="ColorType"/>
  </xsd:sequence>
</xsd:complexType>
Example 13-8. Instance element with mixed content
<letter>Dear <custName>Priscilla Walmsley</custName>,
Unfortunately, we are out of stock of the <prodName>Short-Sleeved
Linen Blouse</prodName> in size <prodSize>10</prodSize> that you
ordered...</letter>
Example 13-9. Complex type with mixed content
<xsd:complexType name="LetterType" mixed="true">
  <xsd:sequence>
    <xsd:element name="custName" type="CustNameType"/>
    <xsd:element name="prodName" type="xsd:string"/>
    <xsd:element name="prodSize" type="SizeType"/>
    <!--...-->
  </xsd:sequence>
</xsd:complexType>
Example 13-10. Instance element with empty content
<color value="blue"/>
Example 13-11. Complex type with empty content
<xsd:complexType name="ColorType">
  <xsd:attribute name="value" type="ColorValueType"/>
</xsd:complexType>
Example 13-12. Element references
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="number" type="ProdNumType"/>
  <xsd:element name="name" type="xsd:string"/>
  <xsd:element name="size" type="SizeType"/>
  <xsd:element name="color" type="ColorType"/>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element ref="number"/>
      <xsd:element ref="name"/>
      <xsd:element ref="size" minOccurs="0"/>
      <xsd:element ref="color" minOccurs="0"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Example 13-13. Complex type with element wildcard
<xsd:complexType name="DescriptionType" mixed="true">
  <xsd:sequence>
    <xsd:any namespace="http://www.w3.org/1999/xhtml"
             minOccurs="0" maxOccurs="unbounded"
             processContents="skip"/>
  </xsd:sequence>
</xsd:complexType>
Example 13-14. Instance with processContents of skip
<catalog xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <description>
    This shirt is the <xhtml:b>best-selling</xhtml:b> shirt in
    our catalog! <xhtml:br/> Note: runs large.
  </description>
  <!--...-->
</catalog>
Example 13-15. Instance with processContents of strict
<catalog xmlns:xhtml="http://www.w3.org/1999/xhtml"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://www.w3.org/1999/xhtml
                             xhtml.xsd">
  <description>
    This shirt is the <xhtml:b>best-selling</xhtml:b> shirt
    in our catalog! <xhtml:br/> Note: runs large.
  </description>
  <!--...-->
</catalog>
Example 13-16. Legal duplication of element-type names
<xsd:complexType name="ProductType">
  <xsd:choice>
    <xsd:sequence>
      <xsd:element name="number" type="ProdNumType"/>
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:choice>
</xsd:complexType>
Example 13-17. Illegal duplication of element-type names
<xsd:complexType name="ProductType">
  <xsd:choice>
    <xsd:sequence>
      <xsd:element name="number" type="ProdNumType"/>
      <xsd:element name="name" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
    <xsd:element name="name" type="xsd:token"/>
  </xsd:choice>
</xsd:complexType>
Example 13-18. A sequence group
<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>
Example 13-19. Valid instances of ProductType
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size system="US-DRESS">10</size>
  <color value="blue"/>
</product>
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
</product>
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <color value="blue"/>
</product>
Example 13-20. Invalid instance of ProductType
<product>
  <number>557</number>
  <size system="US-DRESS">10</size>
  <name>Short-Sleeved Linen Blouse</name>
  <color value="blue"/>
</product>
Example 13-21. A choice group
<xsd:complexType name="ItemsType">
  <xsd:choice>
    <xsd:element name="shirt" type="ShirtType"/>
    <xsd:element name="hat" type="HatType"/>
    <xsd:element name="umbrella" type="UmbrellaType"/>
  </xsd:choice>
</xsd:complexType>
Example 13-22. Valid instances of ItemsType
<items>
  <shirt>...</shirt>
</items>
<items>
  <hat>...</hat>
</items>
Example 13-23. A repeating choice group
<xsd:complexType name="ItemsType">
  <xsd:choice minOccurs="0" maxOccurs="unbounded">
    <xsd:element name="shirt" type="ShirtType"/>
    <xsd:element name="umbrella" type="UmbrellaType"/>
    <xsd:element name="hat" type="HatType"/>
  </xsd:choice>
</xsd:complexType>
Example 13-24. Valid instance of ItemsType
<items>
  <shirt>...</shirt>
  <hat>...</hat>
  <umbrella>...</umbrella>
  <shirt>...</shirt>
  <shirt>...</shirt>
</items>
Example 13-25. Multiple nested groups
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:choice minOccurs="0" maxOccurs="unbounded">
      <xsd:element name="size" type="SizeType"/>
      <xsd:element name="color" type="ColorType"/>
    </xsd:choice>
  </xsd:sequence>
</xsd:complexType>
Example 13-26. An all group
<xsd:complexType name="ProductType">
  <xsd:all>
    <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:all>
</xsd:complexType>
Example 13-27. Valid instances of ProductType using an all group
<product>
  <color value="blue"/>
  <size system="US-DRESS">10</size>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
</product>
<product>
  <name>Short-Sleeved Linen Blouse</name>
  <number>557</number>
</product>
Example 13-28. An illegal all group
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:all>
      <xsd:element name="size" type="SizeType" minOccurs="0"/>
      <xsd:element name="color" type="ColorType" minOccurs="0"/>
    </xsd:all>
  </xsd:sequence>
</xsd:complexType>
Example 13-29. Complex type with a named model group reference
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:group ref="DescriptionGroup"/>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
  </xsd:sequence>
</xsd:complexType>
Example 13-30. Illegal non-deterministic content model
<xsd:complexType name="AOrBOrBothType">
  <xsd:choice>
    <xsd:element name="a" type="xsd:string"/>
    <xsd:element name="b" type="xsd:string"/>
    <xsd:sequence>
      <xsd:element name="a" type="xsd:string"/>
      <xsd:element name="b" type="xsd:string"/>
    </xsd:sequence>
  </xsd:choice>
</xsd:complexType>
Example 13-31. Deterministic content model
<xsd:complexType name="AOrBOrBothType">
  <xsd:choice>
    <xsd:sequence>
      <xsd:element name="a" type="xsd:string"/>
      <xsd:element name="b" type="xsd:string" minOccurs="0"/>
    </xsd:sequence>
    <xsd:element name="b" type="xsd:string"/>
  </xsd:choice>
</xsd:complexType>
Example 13-32. Local attribute declaration
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <!--...-->
  </xsd:sequence>
  <xsd:attribute name="effDate" type="xsd:date"
                 default="1900-01-01"/>
</xsd:complexType>
Example 13-33. Attribute reference
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:attribute name="effDate" type="xsd:date"
                 default="1900-01-01"/>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
    <!--...-->
    </xsd:sequence>
    <xsd:attribute ref="effDate" default="2000-12-31"/>
  </xsd:complexType>
</xsd:schema>
Example 13-34. Complex type with attribute wildcard
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <!--...-->
  </xsd:sequence>
  <xsd:anyAttribute namespace="##other" processContents="lax"/>
</xsd:complexType>
Example 13-35. Complex type with attribute group reference
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <!--...-->
  </xsd:sequence>
  <xsd:attributeGroup ref="IdentifierGroup"/>
  <xsd:attribute name="effDate" type="xsd:date"/>
</xsd:complexType>
Datypic XML Schema Services