How XML Schema Validates ISO 8601 DateTime Strings
XML Schema (XSD) validates ISO 8601 extended formatted timestamps
primarily through its built-in primitive data type,
xs:dateTime. This article explains the lexical rules that
XSD parsers enforce to validate these date and time representations,
covering the required components, calendar constraints, timezone
offsets, and custom validation facets available for granular
control.
The Lexical Format of
xs:dateTime
The xs:dateTime data type enforces a strict subset of
the ISO 8601 extended format. The required lexical representation
follows the pattern:
[-]CCYY-MM-DDThh:mm:ss[.sss][Z|(+|-)hh:mm]
An XML Schema validator decomposes this string into specific components and evaluates each against predetermined rules:
- Date Component (
CCYY-MM-DD): Represents the century, year, month, and day separated by hyphens. The year must be at least four digits (CCYY), with an optional leading minus sign for BCE years. The month (MM) must range from01to12, and the day (DD) must range from01to31. - Separator (
T): A mandatory capital letterTacting as a delimiter between the date and the time components. - Time Component (
hh:mm:ss[.sss]): Represents hours, minutes, and seconds separated by colons. Hours (hh) must range from00to23, minutes (mm) from00to59, and seconds (ss) from00to59. Optional fractional seconds (.sss) can follow with arbitrary precision. - Timezone Offset (
Zor(+|-)hh:mm): An optional indicator representing Coordinated Universal Time (UTC) withZ, or a specific offset from UTC using a sign followed by two-digit hours and two-digit minutes (e.g.,+02:00or-05:00).
Calendar and Logical Constraint Checking
Beyond pattern matching, an XML Schema processor verifies the logical validity of the date:
- Days per Month: The validator enforces the exact number of days in each month (e.g., April must not exceed 30 days).
- Leap Years: February 29th is validated against the Gregorian leap year calculation rules (divisible by 4, except for century years not divisible by 400).
- Leap Seconds: The second field accepts values up to
60in schemas supporting leap seconds, though standard operations normalize or limit values to59.999....
Timezone Processing
When a timezone is provided, the validator checks that the hour
offset does not exceed 14:00 (positive or negative). In
schema-aware operations, timestamps with timezones are converted to a
normalized UTC timeline, allowing the processor to compare relative
chronological ordering accurately. If a timestamp lacks a timezone, it
is treated as an undetermined local time.
Applying Custom Facets
To further restrict xs:dateTime values beyond standard
ISO 8601 validation, developers can apply XSD constraining facets:
xs:minInclusive/xs:maxInclusive: Enforces boundary dates, ensuring timestamps fall within a specific historical or future window.xs:explicitTimezone: Forces the schema to require (required), forbid (prohibited), or optionally accept (optional) timezone information (available in XSD 1.1).xs:pattern: Allows regular expressions to enforce additional formatting restrictions, such as limiting fractional seconds to exactly three digits (\.\d{3}).