Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st 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
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="items" type="ItemsType"/> <xsd:complexType name="ItemsType"> <xsd:sequence> <xsd:element ref="product" maxOccurs="unbounded"/> </xsd:sequence> </xsd:complexType> <xsd:element name="product" type="ProductType"/> <xsd:complexType name="ProductType"> <xsd:sequence> <xsd:element ref="number"/> <xsd:element ref="name"/> </xsd:sequence> </xsd:complexType> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="shirt" type="ShirtType" substitutionGroup="product"/> <xsd:complexType name="ShirtType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:sequence> <xsd:element name="size" type="ShirtSizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:element name="hat" substitutionGroup="product"> <xsd:complexType> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:sequence> <xsd:element name="size" type="HatSizeType"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> </xsd:element> <xsd:element name="umbrella" substitutionGroup="product"/> <!--...--> </xsd: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>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="number"/> <xsd:element name="skuNumber" type="SKUType" substitutionGroup="number"/> <xsd:element name="productID" type="ProductIDType" substitutionGroup="number"/> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="items" type="ItemsType"/> <xsd:complexType name="ItemsType"> <xsd:group ref="ProductGroup" maxOccurs="unbounded"/> </xsd:complexType> <xsd:group name="ProductGroup"> <xsd:choice> <xsd:element name="product" type="ProductType"/> <xsd:element name="shirt" type="ShirtType"/> <xsd:element name="hat" type="HatType"/> <xsd:element name="umbrella" type="ProductType"/> </xsd:choice> </xsd:group> <!--...--> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:complexType name="ShirtType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:sequence> <xsd:element name="size" type="ShirtSizeType"/> <xsd:element name="color" type="ColorType"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="HatType"> <xsd:complexContent> <xsd:extension base="ProductType"> <xsd:sequence> <xsd:element name="size" type="HatSizeType"/> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="UmbrellaType"> <xsd:complexContent> <xsd:extension base="ProductType"/> </xsd:complexContent> </xsd:complexType> <!--...--> </xsd: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>
<xsd:element name="product" type="ProductType" final="#all"/> <xsd:element name="items" type="ItemsType" final="extension"/> <xsd:element name="color" type="ColorType" final="restriction"/> <xsd:element name="size" type="SizeType" final=""/>
<xsd:element name="product" type="ProductType" block="#all"/> <xsd:element name="hat" type="HatType" block="substitution"/>
<xsd:element name="product" type="ProductType" abstract="true"/>