Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st 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>
<xsd:element name="catalog" type="CatalogType"> <xsd:unique name="prodNumKey"> <xsd:selector xpath="*/product"/> <xsd:field xpath="number"/> </xsd:unique> </xsd: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-02"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <price currency="USD">39.99</price> </product> <product effDate="2001-04-02"> <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>
<xsd:element name="catalog" type="CatalogType"> <xsd:unique name="dateAndProdNumKey"> <xsd:selector xpath="department/product"/> <xsd:field xpath="number"/> <xsd:field xpath="@effDate"/> </xsd:unique> </xsd:element>
<xsd:element name="catalog" type="CatalogType"> <xsd:key name="prodNumKey"> <xsd:selector xpath="*/product"/> <xsd:field xpath="number"/> </xsd:key> </xsd: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>
<xsd:element name="order" type="OrderType"> <xsd:keyref name="prodNumKeyRef" refer="prodNumKey"> <xsd:selector xpath="items/*"/> <xsd:field xpath="@number"/> </xsd:keyref> <xsd:key name="prodNumKey"> <xsd:selector xpath=".//product"/> <xsd:field xpath="number"/> </xsd:key> </xsd:element>
<xsd:element name="catalog" type="CatalogType"> <xsd:unique name="prodNumKey"> <xsd:selector xpath="department"/> <xsd:field xpath="product/number"/> </xsd:unique> </xsd:element>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://example.org/prod" targetNamespace="http://example.org/prod"> <xsd:element name="catalog" type="prod:CatalogType"> <xsd:unique name="dateAndProdNumKey"> <xsd:selector xpath="prod:department/prod:product"/> <xsd:field xpath="prod:number"/> <xsd:field xpath="@effDate"/> </xsd:unique> </xsd:element> <xsd:element name="department" type="prod:DepartmentType"/> <xsd:element name="product" type="prod:ProductType"/> <xsd:element name="number" type="prod:ProdNumType"/> <!--...--> </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="catalog" type="CatalogType"> <xsd:unique name="dateAndProdNumKey"> <xsd:selector xpath="department/product"/> <xsd:field xpath="number"/> <xsd:field xpath="@effDate"/> </xsd:unique> </xsd:element> <xsd:element name="department" type="DepartmentType"/> <xsd:element name="product" type="ProductType"/> <xsd:element name="number" type="prod:ProdNumType"/> <!--...--> </xsd:schema>