Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 12: 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 (chapter12.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 (chapter12.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 12-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 12-2. Named complex type
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="SizeType"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="product" type="ProductType"/>
Example 12-3. Anonymous complex type
<xs:element name="product">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="size" type="SizeType"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>
Example 12-4. Instance element with simple content
<size system="US-DRESS">10</size>
Example 12-5. Complex type with 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>
Example 12-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 12-7. Complex type with element-only content
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="SizeType"/>
    <xs:element name="color" type="ColorType"/>
  </xs:sequence>
</xs:complexType>
Example 12-8. Instance element with mixed content
<desc>This is our <i>best-selling</i> shirt.
 <b>Note: </b> runs <u>large</u>.</desc>
Example 12-9. Complex type with mixed content
<xs:element name="desc" type="DescType"/>
<xs:complexType name="DescType" mixed="true">
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="i" type="xs:string"/>
    <xs:element name="b" type="xs:string"/>
    <xs:element name="u" type="xs:string"/>
    <!--...-->
  </xs:choice>
</xs:complexType>
Example 12-10. Instance element with empty content
<color value="blue"/>
Example 12-11. Complex type with empty content
<xs:complexType name="ColorType">
  <xs:attribute name="value" type="ColorValueType"/>
</xs:complexType>
Example 12-12. Element references
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="number" type="xs:integer"/>
  <xs:element name="name" type="xs:string"/>
  <xs:element name="size" type="SizeType"/>
  <xs:element name="color" type="ColorType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element ref="number"/>
      <xs:element ref="name"/>
      <xs:element ref="size" minOccurs="0"/>
      <xs:element ref="color" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <!--...-->
</xs:schema>
Example 12-13. Legal duplication of element names
<xs:complexType name="ProductType">
  <xs:choice>
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:element name="name" type="xs:string"/>
  </xs:choice>
</xs:complexType>
Example 12-14. Illegal duplication of element names
<xs:complexType name="ProductType">
  <xs:choice>
    <xs:sequence>
      <xs:element name="number" type="xs:integer"/>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:element name="name" type="xs:token"/>
  </xs:choice>
</xs:complexType>
Example 12-15. A sequence group
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="SizeType" minOccurs="0"/>
    <xs:element name="color" type="ColorType" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>
Example 12-16. 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 12-17. 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 12-18. Enforcing order for better constraints
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="color" type="ColorType" maxOccurs="3"/>
  </xs:sequence>
</xs:complexType>
Example 12-19. A choice group
<xs:complexType name="ItemsType">
  <xs:choice>
    <xs:element name="shirt" type="ShirtType"/>
    <xs:element name="hat" type="HatType"/>
    <xs:element name="umbrella" type="UmbrellaType"/>
  </xs:choice>
</xs:complexType>
Example 12-20. Valid instances of ItemsType
<items>
  <shirt>...</shirt>
</items>
<items>
  <hat>...</hat>
</items>
Example 12-21. A repeating choice group
<xs:complexType name="ItemsType">
  <xs:choice minOccurs="0" maxOccurs="unbounded">
    <xs:element name="shirt" type="ShirtType"/>
    <xs:element name="umbrella" type="UmbrellaType"/>
    <xs:element name="hat" type="HatType"/>
  </xs:choice>
</xs:complexType>
Example 12-22. Valid instance of ItemsType
<items>
  <shirt>...</shirt>
  <hat>...</hat>
  <umbrella>...</umbrella>
  <shirt>...</shirt>
  <shirt>...</shirt>
</items>
Example 12-23. Multiple nested groups
<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:choice>
  </xs:sequence>
</xs:complexType>
Example 12-24. An all group
<xs:complexType name="ProductType">
  <xs:all>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="SizeType" minOccurs="0"/>
    <xs:element name="color" type="ColorType" minOccurs="0"/>
  </xs:all>
</xs:complexType>
Example 12-25. 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 12-26. An illegal all group
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:all>
      <xs:element name="size" type="SizeType" minOccurs="0"/>
      <xs:element name="color" type="ColorType" minOccurs="0"/>
    </xs:all>
  </xs:sequence>
</xs:complexType>
Example 12-27. Complex type with a named model group reference
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:group ref="DescriptionGroup"/>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
Example 12-28. Illegal nondeterministic content model
<xs:complexType name="AOrBOrBothType">
  <xs:choice>
    <xs:element name="a" type="xs:string"/>
    <xs:element name="b" type="xs:string"/>
    <xs:sequence>
      <xs:element name="a" type="xs:string"/>
      <xs:element name="b" type="xs:string"/>
    </xs:sequence>
  </xs:choice>
</xs:complexType>
Example 12-29. Deterministic content model
<xs:complexType name="AOrBOrBothType">
  <xs:choice>
    <xs:sequence>
      <xs:element name="a" type="xs:string"/>
      <xs:element name="b" type="xs:string" minOccurs="0"/>
    </xs:sequence>
    <xs:element name="b" type="xs:string"/>
  </xs:choice>
</xs:complexType>
Example 12-30. Local attribute declaration
<xs:complexType name="ProductType">
  <xs:sequence>
    <!--...-->
  </xs:sequence>
  <xs:attribute name="effDate" type="xs:date"
                default="1900-01-01"/>
</xs:complexType>
Example 12-31. Attribute reference
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attribute name="effDate" type="xs:date"
                default="1900-01-01"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <!--...-->
    </xs:sequence>
    <xs:attribute ref="effDate" default="2000-12-31"/>
  </xs:complexType>
</xs:schema>
Example 12-32. Complex type with attribute group reference
<xs:complexType name="ProductType">
  <xs:sequence>
    <!--...-->
  </xs:sequence>
  <xs:attributeGroup ref="IdentifierGroup"/>
  <xs:attribute name="effDate" type="xs:date"/>
</xs:complexType>
Example 12-33. Using an element wildcard
<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:any minOccurs="0" maxOccurs="unbounded"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="color" type="xs:string"/>
<xs:element name="desc" type="xs:string"/>
<xs:element name="size" type="xs:integer"/>
Example 12-34. Instance with replacement elements
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <color>blue</color>
  <size>12</size>
  <color>red</color>
</product>
Example 12-35. Complex type with element wildcard
<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>
Example 12-36. 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 12-37. Negative wildcards
<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:any minOccurs="0" maxOccurs="unbounded"
            notNamespace="http://www.w3.org/1999/xhtml"
            notQName="##definedSibling desc size"
            processContents="lax"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="color" type="xs:string"/>
<xs:element name="size" type="xs:integer"/>
Example 12-38. Instance conforming to the schema with negative wildcards
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <color>blue</color>
  <random>not declared in the schema</random>
  <color>red</color>
</product>
Example 12-39. Invalid instance with disallowed namespaces and names
<product xmlns:xhtml="http://www.w3.org/1999/xhtml">
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <size>12</size>
  <desc>Our best-selling shirt</desc>
  <number>12345</number>
  <xhtml:p>Our best-selling shirt</xhtml:p>
</product>
Example 12-40. Defining an interleaved open content model
<xs:element name="product" type="OpenProductType"/>
<xs:complexType name="OpenProductType">
  <xs:openContent>
    <xs:any namespace="##other" processContents="lax"/>
  </xs:openContent>
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
Example 12-41. Instance of an interleaved open content model
<product xmlns:ext="http://datypic.com/extension">
  <ext:something>an extension element</ext:something>
  <number>557</number>
  <ext:something_else>another extension element</ext:something_else>
  <name>Short-Sleeved Linen Blouse</name>
  <ext:something_else>another extension element</ext:something_else>
  <ext:something>an extension element</ext:something>
</product>
Example 12-42. Defining a suffix open content model
<xs:element name="product" type="OpenProductType"/>
<xs:complexType name="OpenProductType">
  <xs:openContent mode="suffix">
    <xs:any namespace="##local" processContents="strict"/>
  </xs:openContent>
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
  </xs:sequence>
</xs:complexType>
<xs:element name="something" type="xs:string"/>
Example 12-43. Instance of a suffix open content model
<product>
  <number>557</number>
  <name>Short-Sleeved Linen Blouse</name>
  <something>an extension element</something>
  <something>an extension element</something>
</product>
Example 12-44. Defining a default open content model
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:defaultOpenContent mode="suffix">
    <xs:any namespace="##local"/>
  </xs:defaultOpenContent>
  <xs:element name="catalog" type="CatalogType"/>
  <xs:complexType name="CatalogType">
    <xs:sequence>
      <xs:element name="product" type="ProductType"
                  maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <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="something" type="xs:string"/>
</xs:schema>
Example 12-45. Instance using default open content model
<catalog>
  <product>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <something>an extension element</something>
    <something>an extension element</something>
  </product>
  <something>an extension element</something>
  <something>an extension element</something>
</catalog>
Example 12-46. Overriding a default open content model
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:defaultOpenContent mode="suffix">
    <xs:any namespace="##local"/>
  </xs:defaultOpenContent>
  <xs:element name="catalog" type="CatalogType"/>
  <xs:complexType name="CatalogType">
    <xs:openContent mode="none"/>
    <xs:sequence>
      <xs:element name="product" type="ProductType"
                  maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
  <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 12-47. Complex type with attribute wildcard
<xs:complexType name="ProductType">
  <xs:sequence>
    <!--...-->
  </xs:sequence>
  <xs:anyAttribute namespace="##other" processContents="lax"/>
</xs:complexType>
Datypic XML Schema Services