Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st 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://example.org/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://example.org/ord"
           xmlns:prod="http://example.org/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://example.org/ord"
       xmlns:prod="http://example.org/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://example.org/ord">
  <number>123ABBCC123</number>
  <items>
    <prod:product xmlns:prod="http://example.org/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://example.org/ord">
  <number>123ABBCC123</number>
  <items>
    <prod:product xmlns:prod="http://example.org/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://example.org/ord"
       xmlns:prod="http://example.org/prod">
  <number>123ABBCC123</number>
  <items>
    <prod:product>
      <prod:number xmlns:prod="http://example.org/prod2">
        557</prod:number>
      <prod:size system="US-DRESS">10</prod:size>
    </prod:product>
  </items>
</order>
Example 3-7. Two attributes with the same local name
<product xmlns="http://example.org/prod"
         xmlns:app="http://example.org/app">
  <number>557</number>
  <size app:system="R32" system="US-DRESS">10</size>
</product>
Example 3-8. Two more attributes with the same local name
<product xmlns="http://example.org/prod"
         xmlns:prod="http://example.org/prod">
  <number>557</number>
  <size system="US-DRESS" prod:system="R32">10</size>
</product>
Example 3-9. Invalid duplicate attributes
<product xmlns:prod="http://example.org/prod"
         xmlns:prod2="http://example.org/prod">
  <number>557</number>
  <size prod:system="US-DRESS" prod2:system="R32">10</size>
</product>
Example 3-10. A summary example
<envelope>
  <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>
Example 3-11. Declaring a target namespace
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:element name="product" type="ProductType"/>
  <xsd:element name="number" type="xsd:integer"/>
  <xsd:element name="size" type="SizeType"/>
  <xsd:complexType name="ProductType">
    <xsd:sequence>
      <xsd:element ref="number"/>
      <xsd:element ref="size"/>
    </xsd:sequence>
  </xsd:complexType>
  <!--...-->
</xsd:schema>
Example 3-12. Prefixed element-type names in an instance
<prod:product xmlns:prod="http://example.org/prod">
  <prod:number>557</prod:number>
  <prod:size>10</prod:size>
</prod:product>
Example 3-13. Declaring the XML Schema Namespace
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="number" type="xsd:integer"/>
  <xsd:element name="size" type="SizeType"/>
  <xsd:simpleType name="SizeType">
    <xsd:restriction base="xsd:integer">
      <!--...-->
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
Example 3-14. Prefixing the XML Schema Namespace
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:element name="number" type="xsd:integer"/>
  <xsd:element name="size" type="SizeType"/>
  <xsd:simpleType name="SizeType">
    <xsd:restriction base="xsd:integer">
      <!--...-->
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
Example 3-15. 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-16. Prefixing the target namespace
<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:prod="http://example.org/prod"
        targetNamespace="http://example.org/prod">
  <element name="number" type="integer"/>
  <element name="size" type="prod:SizeType"/>
  <simpleType name="SizeType">
    <restriction base="integer">
      <!--...-->
    </restriction>
  </simpleType>
</schema>
Example 3-17. Prefixing all namespaces
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:prod="http://example.org/prod"
            targetNamespace="http://example.org/prod">
  <xsd:element name="number" type="xsd:integer"/>
  <xsd:element name="size" type="prod:SizeType"/>
  <xsd:simpleType name="SizeType">
    <xsd:restriction base="xsd:integer">
      <!--...-->
    </xsd:restriction>
  </xsd:simpleType>
</xsd:schema>
Datypic XML Schema Services