Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

2nd edition (September 2012)

Prentice Hall PTR; ISBN: 0132886723

Chapter 18: Redefining and overriding schema components

Book examples

Example 18-1. A simple redefinition
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="DressSizeType">
    <xs:restriction base="xs:integer"/>
  </xs:simpleType>
  <xs:element name="size" type="DressSizeType"/>
  <xs:element name="color" type="xs:string"/>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:simpleType name="DressSizeType">
      <xs:restriction base="DressSizeType">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="16"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:redefine>
  <xs:element name="newSize" type="DressSizeType"/>
</xs:schema>
Example 18-2. Redefining a simple type
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="DressSizeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="size" type="DressSizeType"/>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:simpleType name="DressSizeType">
      <xs:restriction base="DressSizeType">
        <xs:minInclusive value="2"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:redefine>
  <xs:element name="newSize" type="DressSizeType"/>
</xs:schema>
Example 18-3. Redefining a complex type
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <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"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:complexType name="ProductType">
      <xs:complexContent>
        <xs:extension base="ProductType">
          <xs:sequence>
            <xs:element name="color" type="xs:string"/>
          </xs:sequence>
          <xs:attribute name="effDate" type="xs:date"/>
        </xs:extension>
      </xs:complexContent>
    </xs:complexType>
  </xs:redefine>
</xs:schema>
Example 18-4. Redefining a named model group as a subset
prod1.xsd
<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>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:group name="DescriptionGroup">
      <xs:sequence>
        <xs:element name="description" type="xs:string"/>
      </xs:sequence>
    </xs:group>
  </xs:redefine>
</xs:schema>
Example 18-5. Redefining a named model group as a superset
prod1.xsd
<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>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:group name="DescriptionGroup">
      <xs:sequence>
        <xs:group ref="DescriptionGroup"/>
        <xs:element name="notes" type="xs:string"/>
      </xs:sequence>
    </xs:group>
  </xs:redefine>
</xs:schema>
Example 18-6. Redefining an attribute group as a subset
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="version" type="xs:decimal"/>
    <xs:attribute ref="xml:lang"/>
  </xs:attributeGroup>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:attributeGroup name="IdentifierGroup">
      <xs:attribute name="id" type="xs:ID" use="required"/>
      <xs:attribute name="version" type="xs:integer"/>
    </xs:attributeGroup>
  </xs:redefine>
</xs:schema>
Example 18-7. Redefining an attribute group as a superset
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
  <xs:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="version" type="xs:decimal"/>
    <xs:attribute ref="xml:lang"/>
  </xs:attributeGroup>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:redefine schemaLocation="prod1.xsd">
    <xs:attributeGroup name="IdentifierGroup">
      <xs:attributeGroup ref="IdentifierGroup"/>
      <xs:attribute name="effDate" type="xs:date"/>
    </xs:attributeGroup>
  </xs:redefine>
</xs:schema>
Example 18-8. A simple override
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="DressSizeType">
    <xs:restriction base="xs:integer"/>
  </xs:simpleType>
  <xs:element name="size" type="DressSizeType"/>
  <xs:element name="color" type="xs:string"/>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns="http://datypic.com/prod"
           targetNamespace="http://datypic.com/prod">
  <xs:override schemaLocation="prod1.xsd">
    <xs:simpleType name="DressSizeType">
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="16"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:override>
  <xs:element name="newSize" type="DressSizeType"/>
</xs:schema>
Example 18-9. Overriding a simple type
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:simpleType name="DressSizeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="0"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:element name="size" type="DressSizeType"/>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="prod1.xsd">
    <xs:simpleType name="DressSizeType">
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="18"/>
      </xs:restriction>
    </xs:simpleType>
  </xs:override>
  <xs:element name="newSize" type="DressSizeType"/>
</xs:schema>
Example 18-10. Overriding a complex type
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <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:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="prod1.xsd">
    <xs:complexType name="ProductType">
      <xs:sequence>
        <xs:element name="number" type="xs:string"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="color" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:override>
</xs:schema>
Example 18-11. Overriding element and attribute declarations
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="description" type="xs:string"/>
  <xs:attribute name="version" type="xs:decimal"/>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="prod1.xsd">
    <xs:element name="description" type="DescriptionType"/>
    <xs:attribute name="version" type="xs:string" default="1.0"/>
  </xs:override>
  <xs:complexType name="DescriptionType">
    <xs:sequence>
      <xs:element name="source" type="xs:string"/>
      <xs:element name="content" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
Example 18-12. Overriding named groups
prod1.xsd
<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:attributeGroup name="IdentifierGroup">
    <xs:attribute name="id" type="xs:ID" use="required"/>
    <xs:attribute name="version" type="xs:decimal"/>
  </xs:attributeGroup>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="prod1.xsd">
    <xs:group name="DescriptionGroup">
      <xs:sequence>
        <xs:element name="description" type="xs:string"/>
      </xs:sequence>
    </xs:group>
    <xs:attributeGroup name="IdentifierGroup">
      <xs:attribute name="effDate" type="xs:date"/>
      <xs:attribute name="id" type="xs:ID"/>
    </xs:attributeGroup>
  </xs:override>
</xs:schema>
Example 18-13. Risks of overriding types
prod1.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:complexType name="ProductType">
    <xs:sequence>
      <xs:element name="number" type="xs:integer" minOccurs="0"/>
      <xs:element name="name" type="xs:string" minOccurs="0"/>
      <xs:element name="size" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
  <xs:complexType name="ShirtType">
    <xs:complexContent>
      <xs:extension base="ProductType">
        <xs:sequence>
          <xs:element name="color" type="xs:integer"/>
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>
  <xs:complexType name="RestrictedProductType">
    <xs:complexContent>
      <xs:restriction base="ProductType">
        <xs:sequence>
          <xs:element name="number" type="xs:integer"
                      minOccurs="0"/>
          <xs:element name="size" type="xs:string" minOccurs="0"/>
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>
</xs:schema>
prod2.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:override schemaLocation="prod1.xsd">
    <xs:complexType name="ProductType">
      <xs:sequence>
        <xs:element name="number" type="xs:integer"/>
        <xs:element name="name" type="xs:string"/>
        <xs:element name="color" type="xs:string"/>
      </xs:sequence>
    </xs:complexType>
  </xs:override>
</xs:schema>
Datypic XML Schema Services