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

Built-in Datatype - "dateTime"

This section describes what is built-in datatype dateTime and its restriction facets.

The built-in datatype "dateTime" is also used often in XML documents. It represents a date time value described in ISO 8601. A "dateTime" literal value has a pattern of "-?\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d(\.\d+)?(([+-]\d\d:\d\d)|Z)?". It has the following elements:

  • "-?" - Negative sign. Optional.
  • "\d\d\d\d-\d\d-\d\d" - The date part. Required.
  • "\d\d:\d\d:\d\d" - The time part. Required.
  • "(\.\d+)?" - The decimal fraction part of a second. Optional.
  • "(([+-]\d\d:\d\d)|Z)?" - The timezone part. Optional.

The built-in datatype "date" represents the date part of the "dateTime" datatype.

The built-in datatype "time" represents the time part and the second fraction part of the "dateTime" datatype.

"dateTime", "date", and "time" datatypes support the following constraining facets:

  • pattern - Specifies a pattern for the string datatype.
  • enumeration - Specifies a list of string values for the string datatype.
  • whiteSpace - Specifies how white spaces to be handled based on 3 options: "preserve", "replace", or "collapse".
  • maxInclusive - Specifies the maximum value inclusively.
  • maxExclusive - Specifies the maximum value exclusively.
  • minInclusive - Specifies the minimum value inclusively.
  • minExclusive - Specifies the minimum value exclusively.

Here is a sample schema, datetime_datatype.xsd, that uses "dateTime", "date", and "time" datatypes with different restriction facets:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- datetime_datatype.xsd
 - Copyright (c) 2007 by Dr. Herong Yang. All rights reserved.
-->

 <xs:element name="ticket">
  <xs:complexType>

   <!-- Using "minInclusive" to restrict the dateTime -->
   <xs:attribute name="ticketDateTime">
    <xs:simpleType>
     <xs:restriction base="xs:dateTime">
      <xs:minInclusive value="2007-01-01T00:00:00"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

   <!-- Using "maxInclusive" to restrict the date -->
   <xs:attribute name="departureDate">
    <xs:simpleType>
     <xs:restriction base="xs:date">
      <xs:maxInclusive value="2009-12-31"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

   <!-- Using "minInclusive" and "maxInclusive"-->
   <xs:attribute name="departureTime">
    <xs:simpleType>
     <xs:restriction base="xs:time">
      <xs:minInclusive value="05:00:00"/>
      <xs:maxInclusive value="23:00:00"/>
     </xs:restriction>
    </xs:simpleType>
   </xs:attribute>

  </xs:complexType>

 </xs:element>

</xs:schema>

Here is a sample XML document, datetime_datatype.xml, that conforms to datetime_datatype.xsd:

<?xml version="1.0"?>
<ticket
 ticketDateTime="2007-02-02T23:59:59"
 departureDate="2007-03-01"
 departureTime="07:30:00"
/>

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
Built-in Datatype - "dateTime"