Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 4: Schema composition

Full example

This pulls together examples 4-3, 4-4 and 4-5, illustrating include, chameleon include, and import, respectively. Note that only the root element of the instance is prefixed, because all elements in all schema documents are declared locally and no elementFormDefault is specified.

Instance (chapter04.xml)
<ord:order  xmlns:ord="http://example.org/ord"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://example.org/ord chapter04ord1.xsd">
  <number>123ABBCC123</number>
  <customer>
    <name>Priscilla Walmsley</name>
    <number>15466</number>
  </customer>
  <items>
    <product>
      <number>557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size system="US-DRESS">10</size>
      <color value="blue"/>
    </product>
  </items>
</ord:order>
Schema Document 1 (chapter04ord1.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/ord"
            xmlns="http://example.org/ord"
            xmlns:prod="http://example.org/prod">
  <xs:include schemaLocation="chapter04ord2.xsd"/>
  <xs:include schemaLocation="chapter04cust.xsd"/>
  <xs:import namespace="http://example.org/prod"
                 schemaLocation="chapter04prod.xsd"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="OrderNumType"/>
      <xs:element name="customer" type="CustomerType"/>
      <xs:element name="items" type="prod:ItemsType"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Schema Document 2 (chapter04ord2.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xs:simpleType name="OrderNumType">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:schema>
Schema Document 3 (chapter04cust.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="CustomerType">
    <xs:sequence>
      <xs:element name="name" type="CustNameType"/>
      <xs:element name="number" type="xs:integer"/>
    </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="CustNameType">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:schema>
Schema Document 4 (chapter04prod.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element name="product" type="ProductType"/>
    </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:element name="size" type="SizeType"/>
      <xs:element name="color" type="ColorType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="SizeType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="system" type="xs:string"/>
      </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 4-1. Include
ord1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:include schemaLocation="ord2.xsd"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="OrderNumType"/>
      <!--...-->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
ord2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:simpleType name="OrderNumType">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:schema>
Example 4-2. Chameleon include
ord1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:include schemaLocation="cust.xsd"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:string"/>
      <xs:element name="customer" type="CustomerType"/>
      <!--...-->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
cust.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="CustomerType">
    <xs:sequence>
      <xs:element name="name" type="CustNameType"/>
      <!--...-->
    </xs:sequence>
  </xs:complexType>
  <xs:simpleType name="CustNameType">
    <xs:restriction base="xs:string"/>
  </xs:simpleType>
</xs:schema>
Example 4-3. Import
ord1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           xmlns:prod="http://datypic.com/prod"
           targetNamespace="http://datypic.com/ord">
  <xs:import namespace="http://datypic.com/prod"
             schemaLocation="prod.xsd"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:string"/>
      <xs:element name="items" type="prod:ItemsType"/>
      <!--...-->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
prod.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">

  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element name="product" type="ProductType"/>
    </xs:sequence>
  </xs:complexType>
  <!-- ... -->
</xs:schema>
Example 4-4. Multiple levels of import
ord1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           xmlns:prod="http://datypic.com/prod"
           xmlns:ext="http://datypic.com/ext"
           targetNamespace="http://datypic.com/ord">
  <xs:import namespace="http://datypic.com/prod"
             schemaLocation="prod.xsd"/>
  <xs:import namespace="http://datypic.com/ext"
             schemaLocation="extension.xsd"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:string"/>
      <xs:element name="items" type="prod:ItemsType"/>
      <!--...-->
      <xs:element name="extension" type="ext:ExtensionType"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
prod.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           xmlns:ext="http://datypic.com/ext"
           targetNamespace="http://datypic.com/prod">
  <xs:import namespace="http://datypic.com/ext"
             schemaLocation="extension.xsd"/>
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <!-- ... -->
      <xs:element name="extension" type="ext:ExtensionType"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
extension.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ext"
           targetNamespace="http://datypic.com/ext">

  <xs:complexType name="ExtensionType">
    <!-- ... -->
  </xs:complexType>
</xs:schema>
Example 4-5. Multiple imports of the same namespace
root.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/root"
           xmlns:ord="http://datypic.com/ord"
           targetNamespace="http://datypic.com/root">
  <xs:import namespace="http://datypic.com/ord"
             schemaLocation="Summary.xsd"/>
  <xs:import namespace="http://datypic.com/ord"
             schemaLocation="Detail.xsd"/>
  <xs:element name="root" type="RootType"/>
  <xs:complexType name="RootType">
    <xs:sequence>
      <xs:element ref="ord:orderSummary"/>
      <xs:element ref="ord:orderDetails"/>
      <!--...-->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Summary.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:element name="orderSummary"/>
  <!-- ... -->
</xs:schema>
Detail.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:element name="orderDetails"/>
  <!-- ... -->
</xs:schema>
Example 4-6. Proxy schema to avoid multiple imports
root.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/root"
           xmlns:ord="http://datypic.com/ord"
           targetNamespace="http://datypic.com/root">
  <xs:import namespace="http://datypic.com/ord"
             schemaLocation="Orders.xsd"/>
  <xs:element name="root" type="RootType"/>
  <xs:complexType name="RootType">
    <xs:sequence>
      <xs:element ref="ord:orderSummary"/>
      <xs:element ref="ord:orderDetails"/>
      <!-- ... -->
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Orders.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:include schemaLocation="Summary.xsd"/>
  <xs:include schemaLocation="Detail.xsd"/>
  <!-- ... -->
</xs:schema>
Example 4-7. Illegal duplication of element names
ord1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:include schemaLocation="ord2.xsd"/>
  <xs:element name="order" type="OrderType"/>
</xs:schema>
ord2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:element name="order" type="OrderType"/>
</xs:schema>
Example 4-8. Missing component
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:element name="number" type="xs:integer"/>
  <xs:element name="order" type="OrderType"/>
</xs:schema>
Datypic XML Schema Services