Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 3: Namespaces

Full example

This is a more complex example of an instance with elements in multiple namespaces. The namespace declarations are intentionally overcomplicated to illustrate their scope. chapter03env.xsd (which has no target namespace) imports chapter03ord.xsd, which imports chapter03prod.xsd, which imports chapter03prod2.xsd. The form attribute is used to indicate whether element and/or attribute names should be qualified in the instance.

Instance (chapter03.xml)
<envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:noNamespaceSchemaLocation="chapter03env.xsd">
  <order  xmlns="http://example.org/ord"
          xmlns:prod="http://example.org/prod">
    <number>123ABBCC123</number>
    <items>
      <product xmlns="http://example.org/prod">
        <number prod:id="prod557">557</number>
        <name xmlns="">Short-Sleeved Linen Blouse</name>
        <prod:size system="US-DRESS">10</prod:size>
        <prod:color xmlns:prod="http://example.org/prod2"
                    prod:value="blue"/>
      </product>
    </items>
  </order>
</envelope>
Schema Document 1 (chapter03env.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            xmlns:ord="http://example.org/ord">
  <xs:import namespace="http://example.org/ord"
              schemaLocation="chapter03ord.xsd"/>
  <xs:element name="envelope" type="EnvelopeType"/>
  <xs:complexType name="EnvelopeType">
    <xs:sequence>
      <xs:element ref="ord:order" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Schema Document 2 (chapter03ord.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/ord"
            xmlns:ord="http://example.org/ord"
            xmlns:prod="http://example.org/prod"
            elementFormDefault="qualified">
  <xs:import namespace="http://example.org/prod"
              schemaLocation="chapter03prod.xsd"/>
  <xs:element name="order" type="ord:OrderType"/>
  <xs:complexType name="OrderType">
    <xs:sequence>
      <xs:element name="number" type="xs:string"/>
      <xs:element name="items" type="ord: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 3 (chapter03prod.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/prod"
            xmlns="http://example.org/prod"
            xmlns:prod2="http://example.org/prod2"
            elementFormDefault="qualified">
  <xs:import namespace="http://example.org/prod2"
              schemaLocation="chapter03prod2.xsd"/>
  <xs:element name="product" type="ProductType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="ProdNumType"/>
      <xs:element name="name" type="xs:string"
                   form="unqualified"/>
      <xs:element name="size" type="SizeType"/>
      <xs:element ref="prod2:color"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ProdNumType">
    <xs:simpleContent>
      <xs:extension base="xs:integer">
        <xs:attribute name="id" type="xs:ID"
                       form="qualified" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </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>
Schema Document 4 (chapter03prod2.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://example.org/prod2"
            xmlns="http://example.org/prod2">
  <xs:element name="color" type="ColorType"/>
  <xs:complexType name="ColorType">
    <xs:attribute name="value" type="xs:string"
                   form="qualified"/>
  </xs:complexType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 3-1. Namespace declaration
<prod:product xmlns:prod="http://datypic.com/prod">
  <prod:number>557</prod:number>
  <prod:size system="US-DRESS">10</prod:size>
</prod:product>
Example 3-2. Multiple namespace declarations
<ord:order xmlns:ord="http://datypic.com/ord"
           xmlns:prod="http://datypic.com/prod">
  <ord:number>123ABBCC123</ord:number>
  <ord:items>
    <prod:product>
      <prod:number>557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </ord:items>
</ord:order>
Example 3-3. Default namespace declaration
<order xmlns="http://datypic.com/ord"
       xmlns:prod="http://datypic.com/prod">
  <number>123ABBCC123</number>
  <items>
    <prod:product>
      <prod:number>557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </items>
</order>
Example 3-4. Namespace declarations in multiple tags
<order xmlns="http://datypic.com/ord">
  <number>123ABBCC123</number>
  <items>
    <prod:product xmlns:prod="http://datypic.com/prod">
      <prod:number>557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </items>
</order>
Example 3-5. Invalid prefix outside of scope
<order xmlns="http://datypic.com/ord">
  <number>123ABBCC123</number>
  <items>
    <prod:product xmlns:prod="http://datypic.com/prod">
      <prod:number>557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
    <prod:product>
      <prod:number>559</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </items>
</order>
Example 3-6. Overriding a namespace declaration
<order xmlns="http://datypic.com/ord"
       xmlns:prod="http://datypic.com/prod">
  <number>123ABBCC123</number>
  <items>
    <prod:product>
      <prod:number xmlns:prod="http://datypic.com/prod2">
        557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </items>
</order>
Example 3-7. Overriding a default namespace declaration
<order xmlns="http://datypic.com/ord">
  <number>123ABBCC123</number>
  <items>
    <product xmlns="http://datypic.com/prod">
      <number>557</number>
      <size system="US-DRESS">10</size>
    </product>
  </items>
</order>
Example 3-8. Undeclaring a default namespace
<order xmlns="http://datypic.com/ord">
  <number>123ABBCC123</number>
  <items>
    <product xmlns="">
      <number>557</number>
      <size system="US-DRESS">10</size>
    </product>
  </items>
</order>
Example 3-9. Undeclaring a prefixed namespace
<ord:order xmlns:ord="http://datypic.com/ord">
  <ord:number>123ABBCC123</ord:number>
  <ord:items>
    <prod:product xmlns:ord="" xmlns:prod="http://datypic.com/prod">
      <prod:number>557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </ord:items>
</ord:order>
Example 3-10. Two attributes with the same local name
<product xmlns="http://datypic.com/prod"
         xmlns:app="http://datypic.com/app">
  <number>557</number>
  <size app:system="R32" system="US-DRESS">10</size>
</product>
Example 3-11. Two more attributes with the same local name
<product xmlns="http://datypic.com/prod"
         xmlns:prod="http://datypic.com/prod">
  <number>557</number>
  <size system="US-DRESS" prod:system="R32">10</size>
</product>
Example 3-12. Invalid duplicate attributes
<product xmlns:prod="http://datypic.com/prod"
         xmlns:prod2="http://datypic.com/prod">
  <number>557</number>
  <size prod:system="US-DRESS" prod2:system="R32">10</size>
</product>
Example 3-13. A summary example
<envelope>
  <order xmlns="http://datypic.com/ord"
         xmlns:prod="http://datypic.com/prod">
    <number>123ABBCC123</number>
    <items>
      <product xmlns="http://datypic.com/prod">
        <number prod:id="prod557">557</number>
        <name xmlns="">Short-Sleeved Linen Blouse</name>
        <prod:size system="US-DRESS">10</prod:size>
        <prod:color xmlns:prod="http://datypic.com/prod2"
                    prod:value="blue"/>
      </product>
    </items>
  </order>
</envelope>
Example 3-14. Declaring a target namespace
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:element name="product" type="ProductType"/>
  <xs:element name="number" type="xs:integer"/>
  <xs:element name="size" type="SizeType"/>
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element ref="number"/>
      <xs:element ref="size"/>
    </xs:sequence>
  </xs:complexType>
  <!--...-->
</xs:schema>
Example 3-15. Prefixed names in an instance
<prod:product xmlns:prod="http://datypic.com/prod">
  <prod:number>557</prod:number>
  <prod:size>10</prod:size>
</prod:product>
Example 3-16. Declaring the XML Schema Namespace
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="number" type="xs:integer"/>
  <xs:element name="size" type="SizeType"/>
  <xs:simpleType name="SizeType">
    <xs:restriction base="xs:integer">
       <!--...-->
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Example 3-17. Prefixing the XML Schema Namespace
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:element name="number" type="xs:integer"/>
  <xs:element name="size" type="SizeType"/>
  <xs:simpleType name="SizeType">
    <xs:restriction base="xs:integer">
       <!--...-->
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Example 3-18. Invalid absence of prefixes
<schema xmlns="http://www.w3.org/2001/XMLSchema">
  <element name="number" type="integer"/>
  <element name="size" type="SizeType"/>
  <simpleType name="SizeType">
    <restriction base="integer">
       <!--...-->
    </restriction>
  </simpleType>
</schema>
Example 3-19. Prefixing the target namespace
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:prod="http://datypic.com/prod"
        targetNamespace="http://datypic.com/prod">
  <element name="number" type="integer"/>
  <element name="size" type="prod:SizeType"/>
  <simpleType name="SizeType">
    <restriction base="integer">
       <!--...-->
    </restriction>
  </simpleType>
</schema>
Example 3-20. Prefixing all namespaces
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:prod="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:element name="number" type="xs:integer"/>
  <xs:element name="size" type="prod:SizeType"/>
  <xs:simpleType name="SizeType">
    <xs:restriction base="xs:integer">
       <!--...-->
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Datypic XML Schema Services