Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st 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. Illegal duplication of element-type names
ord1.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:include schemaLocation="ord2.xsd"/>
  <xsd:element name="order" type="OrderType"/>
</xsd:schema>
ord2.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:element name="order" type="OrderType"/>
</xsd:schema>
Example 4-2. Missing component
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:element name="number" type="xsd:integer"/>
  <xsd:element name="order" type="OrderType"/>
</xsd:schema>
Example 4-3. Include
ord1.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:include schemaLocation="ord2.xsd"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="OrderNumType"/>
      <!--...-->
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
ord2.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:simpleType name="OrderNumType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
</xsd:schema>
Example 4-4. Chameleon include
ord1.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:include schemaLocation="cust.xsd"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="OrderNumType"/>
      <xsd:element name="customer" type="CustomerType"/>
      <!--...-->
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
cust.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:complexType name="CustomerType">
    <xsd:sequence>
      <xsd:element name="name" type="CustNameType"/>
      <!--...-->
    </xsd:sequence>
  </xsd:complexType>
  <xsd:simpleType name="CustNameType">
    <xsd:restriction base="xsd:string"/>
  </xsd:simpleType>
</xsd:schema>
Example 4-5. Import
ord1.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/ord"
            xmlns:prod="http://example.org/prod"
            targetNamespace="http://example.org/ord">
  <xsd:import namespace="http://example.org/prod"
              schemaLocation="prod.xsd"/>
  <xsd:element name="order" type="OrderType"/>
  <xsd:complexType name="OrderType">
    <xsd:sequence>
      <xsd:element name="number" type="OrderNumType"/>
      <xsd:element name="items" type="prod:ItemsType"/>
      <!--...-->
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
prod.xsd
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
        xmlns="http://example.org/prod"
        targetNamespace="http://example.org/prod">
  <xsd:complexType name="ItemsType">
    <xsd:sequence>
      <xsd:element name="product" type="ProductType"/>
    </xsd:sequence>
  </xsd:complexType>
</xsd:schema>
Datypic XML Schema Services