Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0130655678
1st edition, , Prentice Hall PTR.
Chapter 15: Reusable groups
Full example
This example illustrates named model groups and attribute groups.
<shirt xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="chapter15.xsd" id="P557" version="100"> <description>This is a great shirt.</description> <number>557</number> <name>Short-Sleeved Linen Blouse</name> <size system="US-DRESS">6</size> </shirt>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="ShirtType"> <xs:sequence> <xs:group ref="ProductPropertyGroup" minOccurs="0"/> <xs:element name="size" type="SizeType"/> </xs:sequence> <xs:attributeGroup ref="IdentifierGroup"/> <xs:attribute name="effDate" type="xs:date"/> </xs:complexType> <xs:group name="DescriptionGroup"> <xs:sequence> <xs:element name="description" type="xs:string"/> <xs:element name="comment" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:group> <xs:group name="ProductPropertyGroup"> <xs:sequence> <xs:group ref="DescriptionGroup"/> <xs:element name="number" type="xs:integer"/> <xs:element name="name" type="xs:string"/> </xs:sequence> </xs:group> <xs:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> </xs:attributeGroup> <xs:complexType name="SizeType"> <xs:simpleContent> <xs:extension base="xs:integer"> <xs:attribute name="system" type="xs:token"/> </xs:extension> </xs:simpleContent> </xs:complexType> </xs:schema>
Book examples
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:group name="DescriptionGroup"> <xsd:sequence> <xsd:element name="description" type="xsd:string"/> <xsd:element name="comment" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:group> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:element name="description" type="xsd:string"/> <xsd:element name="comment" type="xsd:string"/> <xsd:group name="DescriptionGroup"> <xsd:sequence> <xsd:element ref="description"/> <xsd:element ref="comment" minOccurs="0"/> </xsd:sequence> </xsd:group> </xsd:schema>
<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:group ref="DescriptionGroup" minOccurs="0"/> <xsd:element ref="items"/> <!--...--> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="PurchaseOrderType"> <xsd:sequence> <xsd:sequence minOccurs="0"> <xsd:element name="description" type="xsd:string"/> <xsd:element name="comment" type="xsd:string" minOccurs="0"/> </xsd:sequence> <xsd:element ref="items"/> <!--...--> </xsd:sequence> </xsd:complexType>
<xsd:complexType name="DescriptionType"> <xsd:group ref="DescriptionGroup"/> <xsd:attribute ref="xml:lang"/> </xsd:complexType>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:group name="DescriptionGroup"> <xsd:all> <xsd:element name="description" type="xsd:string"/> <xsd:element name="comment" type="xsd:string" minOccurs="0"/> </xsd:all> </xsd:group> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:group name="ProductPropertyGroup"> <xsd:sequence> <xsd:group ref="DescriptionGroup"/> <xsd:element name="number" type="ProdNumType"/> <xsd:element name="name" type="xsd:string"/> </xsd:sequence> </xsd:group> <xsd:group name="DescriptionGroup"> <xsd:sequence> <xsd:element ref="description"/> <xsd:element ref="comment" minOccurs="0"/> </xsd:sequence> </xsd:group> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:attributeGroup name="IdentifierGroup"> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute name="version" type="xsd:decimal"/> </xsd:attributeGroup> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:attribute name="id" type="xsd:ID"/> <xsd:attribute name="version" type="xsd:decimal"/> <xsd:attributeGroup name="IdentifierGroup"> <xsd:attribute ref="id" use="required"/> <xsd:attribute ref="version"/> </xsd:attributeGroup> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:attributeGroup name="IdentifierGroup"> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute name="version" type="xsd:decimal"/> <xsd:anyAttribute namespace="##other"/> </xsd:attributeGroup> </xsd:schema>
<xsd:complexType name="ProductType"> <xsd:sequence> <!--...--> </xsd:sequence> <xsd:attributeGroup ref="IdentifierGroup"/> <xsd:attribute name="effDate" type="xsd:date"/> </xsd:complexType>
<xsd:complexType name="ProductType"> <!--...--> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute name="version" type="xsd:decimal"/> <xsd:attribute name="effDate" type="xsd:date"/> </xsd:complexType>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:attributeGroup name="IdentifierGroup"> <xsd:attribute name="id" type="xsd:ID" use="required"/> <xsd:attribute name="version" type="xsd:decimal"/> </xsd:attributeGroup> <xsd:attributeGroup name="VersionGroup"> <xsd:attribute name="version" type="xsd:decimal"/> </xsd:attributeGroup> <xsd:complexType name="ProductType"> <xsd:attributeGroup ref="IdentifierGroup"/> <xsd:attributeGroup ref="VersionGroup"/> </xsd:complexType> </xsd:schema>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <xsd:attributeGroup name="HeaderGroup"> <xsd:attributeGroup ref="IdentifierGroup"/> <xsd:attribute ref="xml:lang"/> </xsd:attributeGroup> </xsd:schema>
<xsd:complexType name="DescribedType"> <xsd:sequence> <xsd:element name="description" type="xsd:string"/> <xsd:element name="comment" type="xsd:string" minOccurs="0"/> </xsd:sequence> </xsd:complexType> <xsd:complexType name="PurchaseOrderType"> <xsd:complexContent> <xsd:extension base="DescribedType"> <xsd:sequence> <xsd:element ref="items"/> <!--...--> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType> <xsd:complexType name="ItemsType"> <xsd:complexContent> <xsd:extension base="DescribedType"> <xsd:sequence> <xsd:element ref="product" maxOccurs="unbounded"/> <!--...--> </xsd:sequence> </xsd:extension> </xsd:complexContent> </xsd:complexType>