Definitive XML Schema
Priscilla Walmsley (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.
<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://example.org/prod"> <prod:number>557</prod:number> <prod:size system="US-DRESS">10</prod:size> </prod:product>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<prod:product xmlns:prod="http://example.org/prod"> <prod:number>557</prod:number> <prod:size>10</prod:size> </prod:product>
<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>
<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>
<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://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>
<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>