XSD Tutorials - Herong's Tutorial Examples - Version 5.11, by Dr. Herong Yang
Facets Supported in "dateTime"
This section describes fundamental facets and constraining facets supported in 'dateTime' and its related built-in datatypes.
Since "dateTime" is the another commonly used simple built-in datatype, let's look at it first to see how many facets are supported in "dateTime".
The "dateTime" datatype has the following settings for its fundamental facets defined:
ordered = partial bounded = false cardinality = countably infinite numeric = false
The "dateTime" datatype supports the following constraining facets:
explicitTimezone = optional whiteSpace = collapse pattern = undefined enumeration = undefined assertions = undefined maxInclusive = undefined maxExclusive = undefined minInclusive = undefined minExclusive = undefined
"date" and "time" datatypes both have the same list of fundamental and constraining facets.
Here is a sample schema, datetime_datatype_facet.xsd, that uses "dateTime", "date", and "time" datatypes with different restriction facets:
<?xml version="1.1"?> <!-- dateTime_datatype_facet.xsd - Copyright (c) 2013, HerongYang.com, All Rights Reserved. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="DateTime_Datatype_Facet"> <xs:complexType> <xs:sequence> <xs:element name="ticket" maxOccurs="unbounded"> <xs:complexType> <!-- Using "minInclusive" to restrict the dateTime --> <xs:attribute name="ticketDateTime"> <xs:simpleType> <xs:restriction base="xs:dateTime"> <xs:minInclusive value="2013-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="2015-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:sequence> </xs:complexType> </xs:element> </xs:schema>
Here is a sample XML document, dateTime_datatype_facet.xml, to test the above XSD document:
<?xml version="1.1"?> <!-- dateTime_datatype_facet.xml - Copyright (c) 2013, HerongYang.com, All Rights Reserved. --> <DateTime_Datatype_Facet> <!-- Valid attribute values --> <ticket ticketDateTime="2013-02-02T23:59:59" departureDate="2013-03-01" departureTime="07:30:00" /> <!-- Inalid attribute values --> <ticket ticketDateTime="2013/02/02T23:59:59" departureDate="2017-03-01" departureTime="03:30:00" /> </DateTime_Datatype_Facet>
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get these errors:
c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator dateTime_datatype_facet.xsd dateTime_datatype_facet.xml Error: Line number: 19 Column number: 6 Message: cvc-datatype-valid.1.2.1: '2013/02/02T23:59:59' is not a valid value for 'dateTime'. Error: Line number: 19 Column number: 6 Message: cvc-attribute.3: The value '2013/02/02T23:59:59' of attribute 'ticketDateTime' on element 'ticket' is not valid with respect to its type, 'null'. Error: Line number: 19 Column number: 6 Message: cvc-maxInclusive-valid: Value '2017-03-01' is not facet-valid with respect to maxInclusive '2015-12-31' for type '#AnonType_departureDateticketDateTime_Datatype_Facet'. Error: Line number: 19 Column number: 6 Message: cvc-attribute.3: The value '2017-03-01' of attribute 'departureDate' on element 'ticket' is not valid with respect to its type, 'null'. Error: Line number: 19 Column number: 6 Message: cvc-minInclusive-valid: Value '03:30:00' is not facet-valid with respect to minInclusive '05:00:00' for type '#AnonType_departureTimeticketDateTime_Datatype_Facet'. Error: Line number: 19 Column number: 6 Message: cvc-attribute.3: The value '03:30:00' of attribute 'departureTime' on element 'ticket' is not valid with respect to its type, 'null'. Failed with errors: 6
Last update: 2013.
Table of Contents
XML Editor and Schema Processor - XMLPad
Java API for XML Processing - JAXP
JAXP - XML Schema (XSD) Validation
Xerces2 Java Parser - Java API of XML Parsers
Introduction of XSD Built-in Datatypes
"string" and Its Derived Datatypes
"decimal" and Its Derived Datatypes
"dateTime" and Its Related Datatypes
Miscellaneous Built-in Datatypes
►Facets, Constraining Facets and Restriction Datatypes
Constructing New Datatypes with Restriction Facets
►Facets Supported in "dateTime"
"simpleType" - Defining Your Own Simple Datatypes
Identity-Constraints: unique, key and keyref
Assertion as Custom Validation Rules
XML Schema Location and Namespace in XML Documents
Overriding Element Types in XML Documents