Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

2nd edition (September 2012)

Prentice Hall PTR; ISBN: 0132886723

Chapter 15: Named groups

Full example

This example illustrates named model groups and attribute groups.

Instance (chapter15.xml)
<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>
Schema (chapter15.xsd)
<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>
Datypic XML Schema Services

Book examples

Example 15-1. Named model group with local element declarations
<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>
Example 15-2. Named model group with element references
<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>
Example 15-3. Referencing a group from a complex type definition
<xs:complexType name="PurchaseOrderType">
  <xs:sequence>
    <xs:group ref="DescriptionGroup" minOccurs="0"/>
    <xs:element ref="items"/>
    <!--...-->
  </xs:sequence>
</xs:complexType>
Example 15-4. Equivalent content model without a named model group reference
<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>
Example 15-5. Group reference at the top level of the content model
<xs:complexType name="DescriptionType">
  <xs:group ref="DescriptionGroup"/>
  <xs:attribute ref="xml:lang"/>
</xs:complexType>
Example 15-6. Group with an all model group
<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>
Example 15-7. Group reference from a group
<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>
Example 15-8. Attribute group with local attribute declarations
<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>
Example 15-9. Attribute group with attribute references
<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>
Example 15-10. Attribute group with a wildcard
<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>
Example 15-11. Referencing an attribute group from a complex type definition
<xs:complexType name="ProductType">
  <xs:sequence>
    <!--...-->
  </xs:sequence>
  <xs:attributeGroup ref="IdentifierGroup"/>
  <xs:attribute name="effDate" type="xs:date"/>
</xs:complexType>
Example 15-12. Equivalent complex type without an attribute group
<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>
Example 15-13. Illegal duplication of attributes
<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>
Example 15-14. Attribute group referencing an attribute group
<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>
Example 15-15. Default attribute group
<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>
Example 15-16. Named groups with a target namespace
<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>
Example 15-17. Named groups across namespaces
ord.xsd
<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>
prod.xsd
<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>
Example 15-18. Reusing content model fragments through derivation
<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>
Datypic XML Schema Services