Definitive XML Schema
Priscilla Walmsley (pwalmsley@datypic.com)
ISBN: 0132886723
2nd edition, , Prentice Hall PTR.
Chapter 15: Named 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
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <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:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="description" type="xs:string"/> <xs:element name="comment" type="xs:string"/> <xs:group name="DescriptionGroup"> <xs:sequence> <xs:element ref="description"/> <xs:element ref="comment" minOccurs="0"/> </xs:sequence> </xs:group> </xs:schema>
<xs:complexType name="PurchaseOrderType"> <xs:sequence> <xs:group ref="DescriptionGroup" minOccurs="0"/> <xs:element ref="items"/> <!--...--> </xs:sequence> </xs:complexType>
<xs:complexType name="PurchaseOrderType"> <xs:sequence> <xs:sequence minOccurs="0"> <xs:element name="description" type="xs:string"/> <xs:element name="comment" type="xs:string" minOccurs="0"/> </xs:sequence> <xs:element ref="items"/> <!--...--> </xs:sequence> </xs:complexType>
<xs:complexType name="DescriptionType"> <xs:group ref="DescriptionGroup"/> <xs:attribute ref="xml:lang"/> </xs:complexType>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:group name="DescriptionGroup"> <xs:all> <xs:element name="description" type="xs:string"/> <xs:element name="comment" type="xs:string" minOccurs="0"/> </xs:all> </xs:group> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <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: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:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> </xs:attributeGroup> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attribute name="id" type="xs:ID"/> <xs:attribute name="version" type="xs:decimal"/> <xs:attributeGroup name="IdentifierGroup"> <xs:attribute ref="id" use="required"/> <xs:attribute ref="version"/> </xs:attributeGroup> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> <xs:anyAttribute namespace="##other"/> </xs:attributeGroup> </xs:schema>
<xs:complexType name="ProductType"> <xs:sequence> <!--...--> </xs:sequence> <xs:attributeGroup ref="IdentifierGroup"/> <xs:attribute name="effDate" type="xs:date"/> </xs:complexType>
<xs:complexType name="ProductType"> <!--...--> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> <xs:attribute name="effDate" type="xs:date"/> </xs:complexType>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> </xs:attributeGroup> <xs:attributeGroup name="VersionGroup"> <xs:attribute name="version" type="xs:decimal"/> </xs:attributeGroup> <xs:complexType name="ProductType"> <xs:attributeGroup ref="IdentifierGroup"/> <xs:attributeGroup ref="VersionGroup"/> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:attributeGroup name="HeaderGroup"> <xs:attributeGroup ref="IdentifierGroup"/> <xs:attribute ref="xml:lang"/> </xs:attributeGroup> <xs:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> </xs:attributeGroup> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/prod" defaultAttributes="prod:IdentifierGroup"> <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="ProductType"> <xs:sequence> <!--...--> </xs:sequence> <xs:attribute name="dept" type="xs:string"/> </xs:complexType> <xs:complexType name="CatalogType" defaultAttributesApply="false"> <xs:sequence> <!--...--> </xs:sequence> <xs:attribute name="catalogNumber" type="xs:integer"/> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod"> <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:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> <xs:anyAttribute namespace="##other"/> </xs:attributeGroup> <xs:complexType name="PurchaseOrderType"> <xs:sequence> <xs:group ref="DescriptionGroup" minOccurs="0"/> <xs:element ref="items"/> <!--...--> </xs:sequence> <xs:attributeGroup ref="IdentifierGroup"/> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://datypic.com/ord" xmlns:prod="http://datypic.com/prod" targetNamespace="http://datypic.com/ord"> <xs:import namespace="http://datypic.com/prod" schemaLocation="prod.xsd"/> <xs:complexType name="PurchaseOrderType"> <xs:sequence> <xs:group ref="prod:DescriptionGroup" minOccurs="0"/> <xs:element ref="items"/> <!--...--> </xs:sequence> <xs:attributeGroup ref="prod:IdentifierGroup"/> </xs:complexType> </xs:schema>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" xmlns="http://datypic.com/prod" targetNamespace="http://datypic.com/prod"> <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:attributeGroup name="IdentifierGroup"> <xs:attribute name="id" type="xs:ID" use="required"/> <xs:attribute name="version" type="xs:decimal"/> <xs:anyAttribute namespace="##other"/> </xs:attributeGroup> </xs:schema>
<xs:complexType name="DescribedType"> <xs:sequence> <xs:element name="description" type="xs:string"/> <xs:element name="comment" type="xs:string" minOccurs="0"/> </xs:sequence> </xs:complexType> <xs:complexType name="PurchaseOrderType"> <xs:complexContent> <xs:extension base="DescribedType"> <xs:sequence> <xs:element ref="items"/> <!--...--> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType> <xs:complexType name="ItemsType"> <xs:complexContent> <xs:extension base="DescribedType"> <xs:sequence> <xs:element ref="product" maxOccurs="unbounded"/> <!--...--> </xs:sequence> </xs:extension> </xs:complexContent> </xs:complexType>