XML Schema Tutorials - Herong's Tutorial Examples
Dr. Herong Yang, Version 4.00

Deriving from Built-in Datatypes - simpleType

This section describes a tutorial example on how to derive simple datatypes by restricting built-in datatypes with simpleType components.

Every built-in datatype has its own default data value range. If you want to restrict the default data value range, you can use restriction facets supported by the built-in datatype.

To apply restriction facets to a built-in datatype, you need to use a Simple Type Definition Component to define a derived datatype based on XML representation rules.

Rule 1. The XML representation of a Simple Type Definition Component is a "simpleType" element.

Rule 2. A "restriction" sub element must be used to specify any restriction facet.

Rule 3. The built-in datatype must be specified as the "base" attribute of the "restriction" element.

Rule 4. One or more restriction facets can be included in the "restriction" element as sub elements.

Here is a schema template that defines a derived datatype from a built-in datatype:

<simpleType name="my_type_name">
 <restriction base="base_type_name">
  Restriction facets
 </restriction>
</simpleType>

<element name="element_name" type="my_type_name"/>

where "element_name" is the name of the XML element, "base_type_name" is a built-in datatype serving as the base datatype, and "my_type_name" is the new datatype derived from the built-in datatype with some restriction facets applied.

Here is a list of restriction facets that are supported by most built-in datatypes:

  • minInclusive - Inclusive minimum value.
  • minExclusive - Exclusive minimum value.
  • maxInclusive - Inclusive maximum value.
  • maxExclusive - Exclusive maximum value.
  • enumeration - One valid value. Repeat this statement to provide multiple values.
  • whiteSpace - How to handle white spaces: "preserve|replace|collapse".
  • pattern - A regular expression the value must match.
  • length - Number of characters.
  • minLength - Minimum number of characters.
  • maxLength - Maximum number of characters.
  • totalDigits - Maximum number of digits.
  • fractionDigits - Maximum number of digits in the fraction part.

The syntax of a facet element is:

<xsd:facet_name value="facet_value">

Besides restricting base datatypes to derive new datatypes, we can also derive new datatypes by define "union" or "list" of base datatypes.

Union: A collection of base datatypes. A value is valid for a union datatype if and only it is valid for one of the base datatypes.

<xsd:simpleType name="my_type_name">
 <union>
  <xsd:restriction base="xsd:type_name_1">
   Restriction facets
  </xsd:restriction>
  <xsd:restriction base="xsd:type_name_2">
   Restriction facets
  </xsd:restriction>
 </union>
</xsd:simpleType>
<xsd:element name="element_name" type="my_type_name"/>

List: A data type for lists of values of a base datatype. The list of values must be white space delimited. For example, the following code:

<xsd:simpleType name="integerList">
 <list>
  <xsd:restriction base="xsd:integer">
   <xsd:minInclusive value="1">
   <xsd:maxInclusive value="49">
  </xsd:restriction>
 </list>
</xsd:simpleType>
<xsd:element name="winning_numbers" type="integerList"/>

defines that "winning_numbers" element is allowed to have a list of number in the range of 1 and 49.

Sections in This Chapter

Overview of XML Schema Built-in Datatypes

List of Built-in Datatypes

Deriving from Built-in Datatypes - simpleType

Built-in Datatype - "string"

Built-in Datatype - "string" Errors

Built-in Datatype - "dateTime"

Built-in Datatype - "dateTime" Errors

Built-in Datatype - "decimal"

Built-in Datatype - "decimal" Errors

Dr. Herong Yang, updated in 2007
Deriving from Built-in Datatypes - simpleType