Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 16: Substitution groups
Full example
This example illustrates substitution groups. The element declarations for shirt, hat
and umbrella
are all in the substitution group whose head is product
.
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="chapter16.xsd"> <product> <number>999</number> <name>Special Seasonal</name> </product> <shirt> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size>10</size> <color value="blue"/> </shirt> <hat> <number>563</number> <name>Ten-Gallon Hat</name> <size>L</size> </hat> <umbrella> <number>443</number> <name>Deluxe Golf Umbrella</name> </umbrella> </items>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="items" type="ItemsType"/> <xs:complexType name="ItemsType"> <xs:sequence> <xs:element ref="product" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <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:sequence> </xs:complexType> <xs:element name="shirt" type="ShirtType" substitutionGroup="product"/> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:sequence> <xs:element name="size" type="ShirtSizeType"/> <xs:element name="color" type="ColorType"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="hat" substitutionGroup="product"> <xs:complexType> <xs:complexContent> <xs:extension base="ProductType"> <xs:sequence> <xs:element name="size" type="HatSizeType"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:element> <xs:element name="umbrella" substitutionGroup="product"/> <xs:complexType name="ShirtSizeType"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType> <xs:complexType name="HatSizeType"> <xs:simpleContent> <xs:extension base="xs:token"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType> <xs:complexType name="ColorType"> <xs:attribute name="value" type="xs:string"/> </xs:complexType> </xs:schema>
Book examples
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="items" type="ItemsType"/> <xs:complexType name="ItemsType"> <xs:sequence> <xs:element ref="product" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> <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:sequence> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="shirt" type="ShirtType" substitutionGroup="product"/> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:sequence> <xs:element name="size" type="ShirtSizeType"/> <xs:element name="color" type="ColorType"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:element name="hat" substitutionGroup="product"> <xs:complexType> <xs:complexContent> <xs:extension base="ProductType"> <xs:sequence> <xs:element name="size" type="HatSizeType"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> </xs:element> <xs:element name="umbrella" substitutionGroup="product"/> <!--...--> </xs:schema>
<items> <product> <number>999</number> <name>Special Seasonal</name> </product> <shirt> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size>10</size> <color value="blue"/> </shirt> <hat> <number>563</number> <name>Ten-Gallon Hat</name> <size>L</size> </hat> <umbrella> <number>443</number> <name>Deluxe Golf Umbrella</name> </umbrella> </items>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="number"/> <xs:element name="skuNumber" type="xs:string" substitutionGroup="number"/> <xs:element name="productID" type="xs:integer" substitutionGroup="number"/> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="product" type="ProductType"/> <xs:element name="discontinuedProduct" type="ProductType"/> <xs:element name="hat" type="HatType" substitutionGroup="product"/> <xs:element name="shirt" type="ShirtType" substitutionGroup="product"/> <xs:element name="umbrella" type="UmbrellaType" substitutionGroup="product discontinuedProduct"/> <!--...--> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="items" type="ItemsType"/> <xs:complexType name="ItemsType"> <xs:group ref="ProductGroup" maxOccurs="unbounded"/> </xs:complexType> <xs:group name="ProductGroup"> <xs:choice> <xs:element name="product" type="ProductType"/> <xs:element name="shirt" type="ShirtType"/> <xs:element name="hat" type="HatType"/> <xs:element name="umbrella" type="ProductType"/> </xs:choice> </xs:group> <!--...--> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="ShirtType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:sequence> <xs:element name="size" type="ShirtSizeType"/> <xs:element name="color" type="ColorType"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="HatType"> <xs:complexContent> <xs:extension base="ProductType"> <xs:sequence> <xs:element name="size" type="HatSizeType"/> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="UmbrellaType"> <xs:complexContent> <xs:extension base="ProductType"/> </xs:complexContent> </xs:complexType> <!--...--> </xs:schema>
<items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <product> <number>999</number> <name>Special Seasonal</name> </product> <product xsi:type="ShirtType"> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size>10</size> <color value="blue"/> </product> <product xsi:type="HatType"> <number>563</number> <name>Ten-Gallon Hat</name> <size>L</size> </product> <product xsi:type="UmbrellaType"> <number>443</number> <name>Deluxe Golf Umbrella</name> </product> </items>
<xs:element name="product" type="ProductType" final="#all"/> <xs:element name="items" type="ItemsType" final="extension"/> <xs:element name="color" type="ColorType" final="restriction"/> <xs:element name="size" type="SizeType" final=""/>
<xs:element name="product" type="ProductType" block="#all"/> <xs:element name="hat" type="HatType" block="substitution"/>
<xs:element name="product" type="ProductType" abstract="true"/>