Definitive XML Schema

Definitive XML Schema

(pwalmsley@datypic.com)

ISBN: 0130655678

1st edition, , Prentice Hall PTR.

Chapter 9: Simple types

Full example

This example illustrates various simple type restrictions using bounds facets, patterns, and enumerations. White space is used in the instance to illustrate that white space processing takes place before validation.

Instance (chapter09.xml)
<sizes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:noNamespaceSchemaLocation="chapter09.xsd">
  <dressSize>06</dressSize>
  <mediumDressSize>12</mediumDressSize>
  <smallDressSize>6</smallDressSize>
  <smlxSize>extra
  large</smlxSize>
  <xsmlxSize>extra    small</xsmlxSize>
</sizes>
Schema (chapter09.xsd)
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="sizes" type="SizesType"/>
  <xs:complexType name="SizesType">
    <xs:choice maxOccurs="unbounded">
      <xs:element name="dressSize" type="DressSizeType"/>
      <xs:element name="mediumDressSize" type="MediumDressSizeType"/>
      <xs:element name="smallDressSize" type="SmallDressSizeType"/>
      <xs:element name="smlxSize" type="SMLXSizeType"/>
      <xs:element name="xsmlxSize" type="XSMLXSizeType"/>
    </xs:choice>
  </xs:complexType>
  <xs:simpleType name="DressSizeType">
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
      <xs:pattern value="\d{1,2}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="MediumDressSizeType">
    <xs:restriction base="DressSizeType">
      <xs:minInclusive value="8"/>
      <xs:maxInclusive value="12"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="SmallDressSizeType">
    <xs:restriction base="DressSizeType">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="6"/>
      <xs:pattern value="\d{1}"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="SMLXSizeType">
    <xs:restriction base="xs:token">
      <xs:enumeration value="small"/>
      <xs:enumeration value="medium"/>
      <xs:enumeration value="large"/>
      <xs:enumeration value="extra large"/>
    </xs:restriction>
  </xs:simpleType>
  <xs:simpleType name="XSMLXSizeType">
    <xs:union memberTypes="SMLXSizeType">
      <xs:simpleType>
        <xs:restriction base="xs:token">
          <xs:enumeration value="extra small"/>
        </xs:restriction>
      </xs:simpleType>
    </xs:union>
  </xs:simpleType>
</xs:schema>
Datypic XML Schema Services

Book examples

Example 9-1. Defining and referencing a named simple type
<xsd:simpleType name="DressSizeType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="2"/>
    <xsd:maxInclusive value="18"/>
  </xsd:restriction>
</xsd:simpleType>
<xsd:element name="size" type="DressSizeType"/>
Example 9-2. Defining an anonymous simple type
<xsd:element name="size">
  <xsd:simpleType>
    <xsd:restriction base="xsd:integer">
      <xsd:minInclusive value="2"/>
      <xsd:maxInclusive value="18"/>
    </xsd:restriction>
  </xsd:simpleType>
</xsd:element>
Example 9-3. Deriving a simple type from a built-in simple type
<xsd:simpleType name="DressSizeType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="2"/>
    <xsd:maxInclusive value="18"/>
    <xsd:pattern value="\d{1,2}"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-4. Deriving a simple type from a user-derived simple type
<xsd:simpleType name="MediumDressSizeType">
  <xsd:restriction base="DressSizeType">
    <xsd:minInclusive value="8"/>
    <xsd:maxInclusive value="12"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-5. Effective definition of MediumDressSizeType
<xsd:simpleType name="MediumDressSizeType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="8"/>
    <xsd:maxInclusive value="12"/>
    <xsd:pattern value="\d{1,2}"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-6. Illegal attempt to extend a simple type
<xsd:simpleType name="SmallDressSizeType">
  <xsd:restriction base="DressSizeType">
    <xsd:minInclusive value="0"/>
    <xsd:maxInclusive value="6"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-7. Fixed facets
<xsd:simpleType name="DressSizeType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="2" fixed="true"/>
    <xsd:maxInclusive value="18" fixed="true"/>
    <xsd:pattern value="\d{1,2}"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-8. Applying the enumeration facet
<xsd:simpleType name="SMLXSizeType">
  <xsd:restriction base="xsd:token">
    <xsd:enumeration value="small"/>
    <xsd:enumeration value="medium"/>
    <xsd:enumeration value="large"/>
    <xsd:enumeration value="extra large"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-9. Restricting an enumeration
<xsd:simpleType name="SMLSizeType">
  <xsd:restriction base="SMLXSizeType">
    <xsd:enumeration value="small"/>
    <xsd:enumeration value="medium"/>
    <xsd:enumeration value="large"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-10. Illegal attempt to extend an enumeration
<xsd:simpleType name="XSMLXSizeType">
  <xsd:restriction base="SMLXSizeType">
    <xsd:enumeration value="extra small"/>
    <xsd:enumeration value="small"/>
    <xsd:enumeration value="medium"/>
    <xsd:enumeration value="large"/>
    <xsd:enumeration value="extra large"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-11. Using a union to extend an enumeration
<xsd:simpleType name="XSMLXSizeType">
  <xsd:union memberTypes="SMLXSizeType">
    <xsd:simpleType>
      <xsd:restriction base="xsd:token">
        <xsd:enumeration value="extra small"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:union>
</xsd:simpleType>
Example 9-12. Enumerating numeric values
Schema:
<xsd:simpleType name="NewSmallDressSizeType">
  <xsd:restriction base="xsd:integer">
    <xsd:enumeration value="2"/>
    <xsd:enumeration value="4"/>
    <xsd:enumeration value="6"/>
  </xsd:restriction>
</xsd:simpleType>
Valid instances:
<size>2</size>
<size>02</size>
Example 9-13. Applying the pattern facet
<xsd:simpleType name="DressSizeType">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="2"/>
    <xsd:maxInclusive value="18"/>
    <xsd:pattern value="\d{1,2}"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-14. Restricting a pattern
<xsd:simpleType name="SmallDressSizeType">
  <xsd:restriction base="DressSizeType">
    <xsd:minInclusive value="2"/>
    <xsd:maxInclusive value="6"/>
    <xsd:pattern value="\d{1}"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-15. Illegal attempt to extend a pattern
<xsd:simpleType name="LongerDressSizeType">
  <xsd:restriction base="DressSizeType">
    <xsd:pattern value="\d{1,3}"/>
  </xsd:restriction>
</xsd:simpleType>
Example 9-16. Valid instances of SMLXSizeType
<size> small </size>
<size>extra
large</size>
Example 9-17. Valid values for the final attribute in simple type definitions
<final="#all"
final="restriction list union"
final="list restriction extension"
final="union">final=""
Example 9-18. Preventing type derivation
<xsd:simpleType name="DressSizeType" final="restriction list">
  <xsd:restriction base="xsd:integer">
    <xsd:minInclusive value="2"/>
    <xsd:maxInclusive value="18"/>
  </xsd:restriction>
</xsd:simpleType>
Datypic XML Schema Services