Definitive XML Schema
Priscilla Walmsley (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
.
<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>
<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>
<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>
Book examples
<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>
<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>
<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>
<product xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="prod.xsd"> <number>557</number> <size>10</size> </product>
<number>557</number>