Definitive XML Schema

Definitive XML Schema

(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.

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