Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

1st edition (December 7, 2001)

Prentice Hall PTR; ISBN: 0130655678

Chapter 21: Extensibility and reuse

Book examples

Example 21-1. Original type using wildcards
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="size" type="SizeType" minOccurs="0"/>
    <xsd:any minOccurs="0" maxOccurs="unbounded"
             namespace="##other" processContents="lax"/>
  </xsd:sequence>
  <xsd:anyAttribute namespace="##other" processContents="skip"/>
</xsd:complexType>
Example 21-2. Instance with extensions
<order xmlns="http://example.org/ord"
       xmlns:spc="http://example.org/spc">
  <product spc:points="100">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <spc:giftWrap>ADULT BDAY</spc:giftWrap>
  </product>
</order>
Example 21-3. Schema for extensions
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns="http://example.org/spc"
            targetNamespace="http://example.org/spc">
  <xsd:element name="giftWrap" type="GiftWrapType"/>
  <xsd:attribute name="points" type="xsd:nonNegativeInteger"/>
</xsd:schema>
Example 21-4. Original type
<xsd:complexType name="ProductType">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="size" type="SizeType" minOccurs="0"/>
  </xsd:sequence>
</xsd:complexType>
Example 21-5. Extended type
<xsd:complexType name="ExtendedProductType">
  <xsd:complexContent>
    <xsd:extension base="ProductType">
      <xsd:sequence>
        <xsd:element ref="spc:giftWrap" minOccurs="0"/>
      </xsd:sequence>
      <xsd:attribute ref="spc:points"/>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 21-6. Instance using extended type
<order xmlns="http://example.org/ord"
       xmlns:spc="http://example.org/spc"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <product spc:points="100" xsi:type="ExtendedProductType">
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
    <spc:giftWrap>ADULT BDAY</spc:giftWrap>
  </product>
</order>
Example 21-7. choice group extension
<xsd:complexType name="ItemsType">
  <xsd:choice maxOccurs="unbounded">
    <xsd:element ref="shirt"/>
    <xsd:element ref="hat"/>
    <xsd:element ref="umbrella"/>
  </xsd:choice>
</xsd:complexType>
<xsd:complexType name="ExpandedItemsType">
  <xsd:complexContent>
    <xsd:extension base="ItemsType">
      <xsd:choice maxOccurs="unbounded">
        <xsd:element ref="sweater"/>
        <xsd:element ref="suit"/>
      </xsd:choice>
    </xsd:extension>
  </xsd:complexContent>
</xsd:complexType>
Example 21-8. Original data type with an abstract element declaration
<xsd:complexType name="ItemsType">
  <xsd:choice maxOccurs="unbounded">
    <xsd:element ref="shirt"/>
    <xsd:element ref="hat"/>
    <xsd:element ref="umbrella"/>
    <xsd:element ref="otherProduct"/>
  </xsd:choice>
</xsd:complexType>
<xsd:element name="otherProduct" type="ProductType"
             abstract="true"/>
Example 21-9. Extension using substitution groups
<xsd:element name="sweater" substitutionGroup="otherProduct"/>
<xsd:element name="suit" substitutionGroup="otherProduct"/>
Example 21-10. Instance using extension via substitution groups
<items>
  <shirt>...</shirt>
  <sweater>...</sweater>
  <shirt>...</shirt>
  <suit>...</suit>
</items>
Example 21-11. Redefined type
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:spc="http://example.org/spc"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:import namespace="http://example.org/spc"/>
  <xsd:redefine schemaLocation="original.xsd">
    <xsd:complexType name="ProductType">
      <xsd:complexContent>
        <xsd:extension base="ProductType">
          <xsd:sequence>
            <xsd:element ref="spc:giftWrap" minOccurs="0"/>
          </xsd:sequence>
          <xsd:attribute ref="spc:points"/>
        </xsd:extension>
      </xsd:complexContent>
    </xsd:complexType>
  </xsd:redefine>
</xsd:schema>
Example 21-12. Original type
<xsd:complexType name="ProductType">
  <xsd:group ref="ProductPropertyGroup"/>
  <xsd:attributeGroup ref="ExtensionGroup"/>
</xsd:complexType>
<xsd:group name="ProductPropertyGroup">
  <xsd:sequence>
    <xsd:element name="number" type="ProdNumType"/>
    <xsd:element name="name" type="xsd:string"/>
    <xsd:element name="size" type="SizeType" minOccurs="0"/>
  </xsd:sequence>
</xsd:group>
<xsd:attributeGroup name="ExtensionGroup"/>
Example 21-13. Redefined named model group and attribute group
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:spc="http://example.org/spc"
            xmlns="http://example.org/ord"
            targetNamespace="http://example.org/ord">
  <xsd:import namespace="http://example.org/spc"/>
  <xsd:redefine schemaLocation="original.xsd">
    <xsd:group name="ProductPropertyGroup">
      <xsd:sequence>
        <xsd:element ref="spc:giftWrap"/>
        <xsd:group ref="ProductPropertyGroup"/>
      </xsd:sequence>
    </xsd:group>
    <xsd:attributeGroup name="ExtensionGroup">
      <xsd:attributeGroup ref="ExtensionGroup"/>
      <xsd:attribute ref="spc:points"/>
    </xsd:attributeGroup>
  </xsd:redefine>
</xsd:schema>
Example 21-14. Instance using redefined named model group and attribute group
<order xmlns="http://example.org/ord"
       xmlns:spc="http://example.org/spc">
  <product spc:points="100">
    <spc:giftWrap>ADULT BDAY</spc:giftWrap>
    <number>557</number>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
  </product>
</order>
Datypic XML Schema Services