Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 5: Instances and schemas

Full example

This example shows how xsi:schemaLocation can be used to pull together multiple schema documents. An import is used in chapter05ord.xsd to show the dependence on the prod namespace, but no schema location is provided in the schema; this type of "dangling" reference to product is allowed. This example also exhibits xsi:nil and xsi:type.

Instance (chapter05.xml)
<ord:order xmlns:ord="http://example.org/ord"
   xmlns:prod="http://example.org/prod"
   xmlns:xs="http://www.w3.org/2001/XMLSchema"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://example.org/prod chapter05prod.xsd
                       http://example.org/ord chapter05ord.xsd">
  <items>
    <prod:product>
      <number xsi:type="xs:short">557</number>
      <name>Short-Sleeved Linen Blouse</name>
      <size xsi:nil="true"></size>
    </prod:product>
  </items>
</ord:order>
Schema Document 1 (chapter05ord.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:import namespace="http://example.org/prod"/>
  <xs:element name="order" type="OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="items" type="ItemsType"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ItemsType">
    <xs:sequence>
      <xs:element ref="prod:product" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Schema Document 2 (chapter05prod.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <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:element name="size" nillable="true" type="SizeType"/>
    </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:schema>
Datypic XML Schema Services

Book examples

Example 5-1. Using an instance attribute
<product xmlns="http://datypic.com/prod"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <number xsi:type="ShortProdNumType">557</number>
  <size>10</size>
</product>
Example 5-2. Using xsi:schemaLocation
<product xmlns="http://datypic.com/prod"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://datypic.com/prod prod.xsd">
  <number>557</number>
  <size>10</size>
</product>
Example 5-3. Using xsi:schemaLocation with multiple pairs
<order xmlns="http://datypic.com/ord"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://datypic.com/prod prod.xsd
                           http://datypic.com/ord ord1.xsd">
  <items>
    <product xmlns="http://datypic.com/prod">
      <number>557</number>
      <size>10</size>
    </product>
  </items>
</order>
Example 5-4. Using xsi:noNamespaceSchemaLocation
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="prod.xsd">
  <number>557</number>
  <size>10</size>
</product>
Example 5-5. A valid instance?
<number>557</number>
Datypic XML Schema Services