Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0132886723

2nd edition, , Prentice Hall PTR.

Chapter 22: Extensibility and reuse

Book examples

Example 22-1. Sample common components library
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://datypic.com/common"
           xmlns="http://datypic.com/common"
           elementFormDefault="qualified">
  <xs:simpleType name="ProductIDType">
    <xs:restriction base="xs:string">
      <xs:pattern value="[A-Z]{2}[0-9]{4}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:complexType name="PriceType">
    <xs:simpleContent>
      <xs:extension base="xs:decimal">
        <xs:attribute name="currency" type="CurrencyCodeType"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="MeasurementType">
    <xs:simpleContent>
      <xs:extension base="xs:decimal">
        <xs:attribute name="units" type="UnitsCodeType"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>
  <xs:complexType name="AddressType">
    <xs:sequence>
      <xs:element name="street" type="xs:string" maxOccurs="3"/>
      <xs:element name="city" type="xs:string"/>
      <xs:element name="state" type="xs:string"/>
      <xs:element name="postalCode" type="xs:string"/>
      <xs:element name="country" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
  <!--...-->
</xs:schema>
Example 22-2. Original type using wildcards
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="xs:integer" minOccurs="0"/>
    <xs:any minOccurs="0" maxOccurs="unbounded"
            namespace="##other" processContents="lax"/>
  </xs:sequence>
  <xs:anyAttribute namespace="##other" processContents="skip"/>
</xs:complexType>
Example 22-3. Instance with extensions
<order xmlns="http://datypic.com/ord"
       xmlns:spc="http://datypic.com/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 22-4. Schema for extensions
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/spc"
           targetNamespace="http://datypic.com/spc">
  <xs:element name="giftWrap" type="xs:string"/>
  <xs:attribute name="points" type="xs:nonNegativeInteger"/>
</xs:schema>
Example 22-5. Original type using open content
<xs:complexType name="ProductType">
  <xs:openContent>
    <xs:any namespace="##other" processContents="lax"/>
  </xs:openContent>
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="xs:integer" minOccurs="0"/>
  </xs:sequence>
  <xs:anyAttribute namespace="##other" processContents="skip"/>
</xs:complexType>
Example 22-6. Instance with open content extensions
<order xmlns="http://datypic.com/ord"
       xmlns:spc="http://datypic.com/spc">
  <product spc:points="100">
    <number>557</number>
    <spc:giftWrap>ADULT BDAY</spc:giftWrap>
    <name>Short-Sleeved Linen Blouse</name>
    <size>10</size>
  </product>
</order>
Example 22-7. Original type
<xs:complexType name="ProductType">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="xs:integer" minOccurs="0"/>
  </xs:sequence>
</xs:complexType>
Example 22-8. Extended type
<xs:complexType name="ExtendedProductType">
  <xs:complexContent>
    <xs:extension base="ProductType">
      <xs:sequence>
        <xs:element ref="spc:giftWrap" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute ref="spc:points"/>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
Example 22-9. Instance using extended type
<order xmlns="http://datypic.com/ord"
       xmlns:spc="http://datypic.com/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 22-10. choice group extension
<xs:complexType name="ItemsType">
  <xs:choice maxOccurs="unbounded">
    <xs:element ref="shirt"/>
    <xs:element ref="hat"/>
    <xs:element ref="umbrella"/>
  </xs:choice>
</xs:complexType>
<xs:complexType name="ExpandedItemsType">
  <xs:complexContent>
    <xs:extension base="ItemsType">
      <xs:choice maxOccurs="unbounded">
        <xs:element ref="sweater"/>
        <xs:element ref="suit"/>
      </xs:choice>
    </xs:extension>
  </xs:complexContent>
</xs:complexType>
Example 22-11. Original type with an abstract element declaration
<xs:complexType name="ItemsType">
  <xs:choice maxOccurs="unbounded">
    <xs:element ref="shirt"/>
    <xs:element ref="hat"/>
    <xs:element ref="umbrella"/>
    <xs:element ref="otherProduct"/>
  </xs:choice>
</xs:complexType>
<xs:element name="otherProduct" type="ProductType"
            abstract="true"/>
Example 22-12. Extension using substitution groups
<xs:element name="sweater" substitutionGroup="otherProduct"/>
<xs:element name="suit" substitutionGroup="otherProduct"/>
Example 22-13. Instance using extension via substitution groups
<items>
  <shirt>...</shirt>
  <sweater>...</sweater>
  <shirt>...</shirt>
  <suit>...</suit>
</items>
Example 22-14. Redefined type
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:spc="http://datypic.com/spc"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:import namespace="http://datypic.com/spc"/>
  <xs:redefine schemaLocation="original.xsd">
    <xs:complexType name="ProductType">
      <xs:complexContent>
        <xs:extension base="ProductType">
          <xs:sequence>
            <xs:element ref="spc:giftWrap" minOccurs="0"/>
          </xs:sequence>
          <xs:attribute ref="spc:points"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>
Example 22-15. Original type
<xs:complexType name="ProductType">
  <xs:group ref="ProductPropertyGroup"/>
  <xs:attributeGroup ref="ExtensionGroup"/>
</xs:complexType>
<xs:group name="ProductPropertyGroup">
  <xs:sequence>
    <xs:element name="number" type="xs:integer"/>
    <xs:element name="name" type="xs:string"/>
    <xs:element name="size" type="xs:integer" minOccurs="0"/>
  </xs:sequence>
</xs:group>
<xs:attributeGroup name="ExtensionGroup"/>
Example 22-16. Redefined named model group and attribute group
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:spc="http://datypic.com/spc"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord">
  <xs:import namespace="http://datypic.com/spc"/>
  <xs:redefine schemaLocation="original.xsd">
    <xs:group name="ProductPropertyGroup">
      <xs:sequence>
        <xs:element ref="spc:giftWrap"/>
        <xs:group ref="ProductPropertyGroup"/>
      </xs:sequence>
    </xs:group>
    <xs:attributeGroup name="ExtensionGroup">
      <xs:attributeGroup ref="ExtensionGroup"/>
      <xs:attribute ref="spc:points"/>
    </xs:attributeGroup>
  </xs:redefine>
</xs:schema>
Example 22-17. Instance using redefined named model group and attribute group
<order xmlns="http://datypic.com/ord"
       xmlns:spc="http://datypic.com/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>
Example 22-18. Overridden type
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:spc="http://datypic.com/spc"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord"
           elementFormDefault="qualified">
  <xs:import namespace="http://datypic.com/spc"/>
  <xs:override schemaLocation="original.xsd">
    <xs:complexType name="ProductType">
      <xs:sequence>
        <xs:element name="number" type="xs:integer"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="size" type="xs:integer" minOccurs="0"/>
        <xs:element ref="spc:giftWrap" minOccurs="0"/>
      </xs:sequence>
      <xs:attribute ref="spc:points"/>
    </xs:complexType>
  </xs:override>
</xs:schema>
Example 22-19. Overridden named model group and attribute group
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:spc="http://datypic.com/spc"
           xmlns="http://datypic.com/ord"
           targetNamespace="http://datypic.com/ord"
           elementFormDefault="qualified">
  <xs:import namespace="http://datypic.com/spc"/>
  <xs:override schemaLocation="original.xsd">
    <xs:group name="ProductPropertyGroup">
      <xs:sequence>
        <xs:element ref="spc:giftWrap"/>
        <xs:element name="number" type="xs:integer"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="size" type="xs:integer" minOccurs="0"/>
      </xs:sequence>
    </xs:group>
    <xs:attributeGroup name="ExtensionGroup">
      <xs:attribute ref="spc:points"/>
    </xs:attributeGroup>
  </xs:override>
</xs:schema>
Example 22-20. Instance using overridden named model group and attribute group
<order xmlns="http://datypic.com/ord"
       xmlns:spc="http://datypic.com/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