Datypic Logo
Definitive XML Schema

Definitive XML Schema

Priscilla Walmsley (pwalmsley@datypic.com)

2nd edition (September 2012)

Prentice Hall PTR; ISBN: 0132886723

Chapter 8: 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 (chapter08.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 (chapter08.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 8-1. Defining and referencing a named simple type
<xs:simpleType name="DressSizeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="2"/>
    <xs:maxInclusive value="18"/>
  </xs:restriction>
</xs:simpleType>
<xs:element name="size" type="DressSizeType"/>
Example 8-2. Defining an anonymous simple type
<xs:element name="size">
  <xs:simpleType>
    <xs:restriction base="xs:integer">
      <xs:minInclusive value="2"/>
      <xs:maxInclusive value="18"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
Example 8-3. Deriving a simple type from a built-in simple type
<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>
Example 8-4. Deriving a simple type from a user-derived simple type
<xs:simpleType name="MediumDressSizeType">
  <xs:restriction base="DressSizeType">
    <xs:minInclusive value="8"/>
    <xs:maxInclusive value="12"/>
  </xs:restriction>
</xs:simpleType>
Example 8-5. Effective definition of MediumDressSizeType
<xs:simpleType name="MediumDressSizeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="8"/>
    <xs:maxInclusive value="12"/>
    <xs:pattern value="\d{1,2}"/>
  </xs:restriction>
</xs:simpleType>
Example 8-6. Illegal attempt to extend a simple type
<xs:simpleType name="SmallDressSizeType">
  <xs:restriction base="DressSizeType">
    <xs:minInclusive value="0"/>
    <xs:maxInclusive value="6"/>
  </xs:restriction>
</xs:simpleType>
Example 8-7. Fixed facets
<xs:simpleType name="DressSizeType">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="2" fixed="true"/>
    <xs:maxInclusive value="18" fixed="true"/>
    <xs:pattern value="\d{1,2}"/>
  </xs:restriction>
</xs:simpleType>
Example 8-8. Union allowing an empty value
<xs:simpleType name="DressSizeType">
  <xs:union>
    <xs:simpleType>
      <xs:restriction base="xs:integer">
        <xs:minInclusive value="2"/>
        <xs:maxInclusive value="18"/>
      </xs:restriction>
    </xs:simpleType>
    <xs:simpleType>
      <xs:restriction base="xs:token">
        <xs:enumeration value=""/>
      </xs:restriction>
    </xs:simpleType>
  </xs:union>
</xs:simpleType>
Example 8-9. Applying the enumeration facet
<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>
Example 8-10. Restricting an enumeration
<xs:simpleType name="SMLSizeType">
  <xs:restriction base="SMLXSizeType">
    <xs:enumeration value="small"/>
    <xs:enumeration value="medium"/>
    <xs:enumeration value="large"/>
  </xs:restriction>
</xs:simpleType>
Example 8-11. Illegal attempt to extend an enumeration
<xs:simpleType name="XSMLXSizeType">
  <xs:restriction base="SMLXSizeType">
    <xs:enumeration value="extra small"/>
    <xs:enumeration value="small"/>
    <xs:enumeration value="medium"/>
    <xs:enumeration value="large"/>
    <xs:enumeration value="extra large"/>
  </xs:restriction>
</xs:simpleType>
Example 8-12. Using a union to extend an enumeration
<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>
Example 8-13. Enumerating numeric values
Schema:
<xs:simpleType name="NewSmallDressSizeType">
  <xs:restriction base="xs:integer">
    <xs:enumeration value="2"/>
    <xs:enumeration value="4"/>
    <xs:enumeration value="6"/>
  </xs:restriction>
</xs:simpleType>
Valid instances:
<size>2</size>
<size>02</size>
Example 8-14. Applying the pattern facet
<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>
Example 8-15. Restricting a pattern
<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>
Example 8-16. Illegal attempt to extend a pattern
<xs:simpleType name="LongerDressSizeType">
  <xs:restriction base="DressSizeType">
    <xs:pattern value="\d{1,3}"/>
  </xs:restriction>
</xs:simpleType>
Example 8-17. Simple type assertion
<xs:simpleType name="EvenDressSizeType">
  <xs:restriction base="DressSizeType">
    <xs:assertion test="$value mod 2 = 0"/>
  </xs:restriction>
</xs:simpleType>
Example 8-18. Explicit time zone
<xs:simpleType name="SpecificTimeType">
  <xs:restriction base="xs:time">
    <xs:explicitTimezone value="required"/>
  </xs:restriction>
</xs:simpleType>
Example 8-19. Valid instances of SMLXSizeType
<size> small </size>
<size>extra
large</size>
Example 8-20. Valid values for the final attribute in simple type definitions
final="#all"
final="restriction list union"
final="list restriction extension"
final="union"
final=""
Example 8-21. Preventing type derivation
<xs:simpleType name="DressSizeType" final="restriction list">
  <xs:restriction base="xs:integer">
    <xs:minInclusive value="2"/>
    <xs:maxInclusive value="18"/>
  </xs:restriction>
</xs:simpleType>
Example 8-22. Using an implementation-defined type
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ext="http://example.org/extensions">
  <xs:element name="anyOrdinalDate" type="ext:ordinalDate"/>
  <xs:element name="recentOrdinalDate" type="OrdinalDateIn2011"/>
  <xs:simpleType name="OrdinalDateIn2011">
    <xs:restriction base="ext:ordinalDate">
      <xs:minInclusive value="2011-001"/>
      <xs:maxInclusive value="2011-365"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Example 8-23. Using the Saxon preprocess facet
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:saxon="http://saxon.sf.net/">
  <xs:simpleType name="SMLXSizeType">
    <xs:restriction base="xs:token">
      <saxon:preprocess action="upper-case($value)"/>
      <xs:enumeration value="SMALL"/>
      <xs:enumeration value="MEDIUM"/>
      <xs:enumeration value="LARGE"/>
      <xs:enumeration value="EXTRA LARGE"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>
Datypic XML Schema Services