Definitive XML Schema
Priscilla Walmsley (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.
<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>
<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>
<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>
<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>
<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>
Book examples
<prod:product xmlns:prod="http://datypic.com/prod"> <prod:number>557</prod:number> <prod:size system="US-DRESS">10</prod:size> </prod:product>
<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>
<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>
<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>
<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>
<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>
<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>
<order xmlns="http://datypic.com/ord"> <number>123ABBCC123</number> <items> <product xmlns=""> <number>557</number> <size system="US-DRESS">10</size> </product> </items> </order>
<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>
<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>
<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>
<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>
<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>
<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>
<prod:product xmlns:prod="http://datypic.com/prod"> <prod:number>557</prod:number> <prod:size>10</prod:size> </prod:product>
<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>
<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>
<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>
<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>
<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>