Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 17: Identity constraints
Full example
This example illustrates identity constraints. Each product
element must have a number
child whose value is unique within order
. Each child of items
must have a number
attribute whose value matches one of these unique product numbers.
<order xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="chapter17.xsd"> <number>123ABBCC123</number> <items> <shirt number="557"> <quantity>1</quantity> <color value="blue"/> </shirt> <shirt number="557"> <quantity>1</quantity> <color value="sage"/> </shirt> <hat number="563"> <quantity>1</quantity> </hat> </items> <products> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <price currency="USD">29.99</price> </product> <product> <number>563</number> <name>Ten-Gallon Hat</name> <price currency="USD">69.99</price> </product> </products> </order>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="order" type="OrderType"> <xs:keyref name="prodNumKeyRef" refer="prodNumKey"> <xs:selector xpath="items/*"/> <xs:field xpath="@number"/> </xs:keyref> <xs:key name="prodNumKey"> <xs:selector xpath=".//product"/> <xs:field xpath="number"/> </xs:key> </xs:element> <xs:complexType name="OrderType"> <xs:sequence> <xs:element name="number" type="xs:string"/> <xs:element name="items" type="ItemsType"/> <xs:element name="products" type="ProductsType"/> </xs:sequence> </xs:complexType> <xs:complexType name="ItemsType"> <xs:choice maxOccurs="unbounded"> <xs:element name="shirt" type="ProductOrderType"/> <xs:element name="hat" type="ProductOrderType"/> </xs:choice> </xs:complexType> <xs:complexType name="ProductOrderType"> <xs:sequence> <xs:element name="quantity" type="xs:integer"/> <xs:element name="color" type="ColorType" minOccurs="0"/> </xs:sequence> <xs:attribute name="number" type="xs:integer"/> </xs:complexType> <xs:complexType name="ProductsType"> <xs:sequence> <xs:element name="product" type="ProductType" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <xs:complexType name="ProductType"> <xs:sequence> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> <xs:element name="price" type="PriceType"/> </xs:sequence> </xs:complexType> <xs:complexType name="ColorType"> <xs:attribute name="value" type="xs:string"/> </xs:complexType> <xs:complexType name="PriceType"> <xs:simpleContent> <xs:extension base="xs:decimal"> <xs:attribute name="currency" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:schema>
Book examples
<catalog> <department number="021"> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <price currency="USD">29.99</price> </product> <product> <number>563</number> <name>Ten-Gallon Hat</name> <price currency="USD">69.99</price> </product> <product> <number>443</number> <name>Deluxe Golf Umbrella</name> <price currency="USD">49.99</price> </product> </department> </catalog>
<xs:element name="catalog" type="CatalogType"> <xs:unique name="prodNumKey"> <xs:selector xpath="*/product"/> <xs:field xpath="number"/> </xs:unique> </xs:element>
<catalog> <department number="021"> <product effDate="2000-02-27"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <price currency="USD">29.99</price> </product> <product effDate="2001-04-12"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <price currency="USD">39.99</price> </product> <product effDate="2001-04-12"> <number>563</number> <name>Ten-Gallon Hat</name> <price currency="USD">69.99</price> </product> <product> <number>443</number> <name>Deluxe Golf Umbrella</name> <price currency="USD">49.99</price> </product> </department> </catalog>
<xs:element name="catalog" type="CatalogType"> <xs:unique name="dateAndProdNumKey"> <xs:selector xpath="department/product"/> <xs:field xpath="number"/> <xs:field xpath="@effDate"/> </xs:unique> </xs:element>
<xs:element name="catalog" type="CatalogType"> <xs:key name="prodNumKey"> <xs:selector xpath="*/product"/> <xs:field xpath="number"/> </xs:key> </xs:element>
<order> <number>123ABBCC123</number> <items> <shirt number="557"> <quantity>1</quantity> <color value="blue"/> </shirt> <shirt number="557"> <quantity>1</quantity> <color value="sage"/> </shirt> <hat number="563"> <quantity>1</quantity> </hat> </items> <products> <product> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <price currency="USD">29.99</price> </product> <product> <number>563</number> <name>Ten-Gallon Hat</name> <price currency="USD">69.99</price> </product> </products> </order>
<xs:element name="order" type="OrderType"> <xs:keyref name="prodNumKeyRef" refer="prodNumKey"> <xs:selector xpath="items/*"/> <xs:field xpath="@number"/> </xs:keyref> <xs:key name="prodNumKey"> <xs:selector xpath=".//product"/> <xs:field xpath="number"/> </xs:key> </xs:element>
<xs:element name="catalog" type="CatalogType"> <xs:unique name="prodNumKey"> <xs:selector xpath="department"/> <xs:field xpath="product/number"/> </xs:unique> </xs:element>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/prod"> <xs:element name="catalog" type="prod:CatalogType"> <xs:unique name="dateAndProdNumKey"> <xs:selector xpath="prod:department/prod:product"/> <xs:field xpath="prod:number"/> <xs:field xpath="@effDate"/> </xs:unique> </xs:element> <xs:element name="department" type="prod:DepartmentType"/> <xs:element name="product" type="prod:ProductType"/> <xs:element name="number" type="xs:integer"/> <!--...--> </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="catalog" type="CatalogType"> <xs:unique name="dateAndProdNumKey"> <xs:selector xpath="department/product"/> <xs:field xpath="number"/> <xs:field xpath="@effDate"/> </xs:unique> </xs:element> <xs:element name="department" type="DepartmentType"/> <xs:element name="product" type="ProductType"/> <xs:element name="number" type="xs:integer"/> <!--...--> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod" xpathDefaultNamespace="http://datypic.com/prod"> <xs:element name="catalog" type="CatalogType"> <xs:unique name="dateAndProdNumKey"> <xs:selector xpath="department/product"/> <xs:field xpath="number"/> <xs:field xpath="@effDate"/> </xs:unique> </xs:element> <xs:element name="department" type="DepartmentType"/> <xs:element name="product" type="ProductType"/> <xs:element name="number" type="xs:integer"/> <!--...--> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod" xpathDefaultNamespace="http://datypic.com/prod"> <xs:element name="catalog" type="CatalogType"> <xs:unique name="dateAndProdNumKey"> <xs:selector xpath="department/product"/> <xs:field xpath="number"/> <xs:field xpath="@effDate"/> </xs:unique> </xs:element> <xs:element name="discontinuedProductList" type="CatalogType"> <xs:unique ref="dateAndProdNumKey"/> </xs:element> <!--...--> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://datypic.com/prod" xmlns="http://datypic.com/prod" xpathDefaultNamespace="http://datypic.com/prod"> <xs:complexType name="CatalogListType"> <xs:sequence> <xs:element name="catalog" type="CatalogType" maxOccurs="unbounded"> <xs:unique name="dateAndProdNumKey"> <xs:selector xpath="department/product"/> <xs:field xpath="number"/> <xs:field xpath="@effDate"/> </xs:unique> </xs:element> </xs:sequence> </xs:complexType> <xs:complexType name="RestrictedCatalogListType"> <xs:complexContent> <xs:restriction base="CatalogListType"> <xs:sequence> <xs:element name="catalog" type="CatalogType" maxOccurs="1"> <xs:unique ref="dateAndProdNumKey"/> </xs:element> </xs:sequence> </xs:restriction> </xs:complexContent> </xs:complexType> <!--...--> </xs:schema>