XSD Tutorials - Herong's Tutorial Examples - v5.23, by Herong Yang
"date" Datatype Values and Representations
This section describes the built-in primitive datatype, 'date' that represents dates in Gregorian calendar. Leading and trailing whitespaces are allowed and trimmed. Timezone offset is optional.
The "dateTime" datatype can be easily divided into 2 datatypes:
Let's look at "date" first in this tutorial.
"date" is a built-in datatype that uses the date/time seven-property model to represent a date in the Gregorian calendar system with these rules:
To verify these rules, I wrote this simple XSD document that uses "date" datatype to declare XML elements:
<?xml version="1.1"?> <!-- date_datatype_test.xsd - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:element name="Date_Datatype_Test"> <xs:complexType> <xs:sequence> <xs:element name="Date" type="xs:date" maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element> </xs:schema>
Here is a sample XML document that can be used to test these declarations:
<?xml version="1.1"?> <!-- date_datatype_test.xml - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved. --> <Date_Datatype_Test> <!-- 4 valid "Date" elements --> <Date> 1971-05-16Z </Date> <Date> 0001-05-16Z </Date> <Date> -1971000-05-16 </Date> <Date>-0123-05-16-05:00</Date> <!-- 4 invalid "Date" elements --> <Date> 05-16 </Date> <Date> 1971-05-16T00:00:00 </Date> <Date> 12-05-16 </Date> <Date>001234-05-31</Date> </Date_Datatype_Test>
When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get 4 groups of errors for 4 invalid XML elements:
herong> java XsdSchemaValidator ^^^ date_datatype_test.xsd date_datatype_test.xml Error: Line number: 14 Column number: 31 Message: cvc-datatype-valid.1.2.1: '05-16' is not a valid value for 'date'. (Missing "year") Error: Line number: 14 Column number: 31 Message: cvc-type.3.1.3: The value ' 05-16 ' of element 'Date' is not valid. Error: Line number: 15 Column number: 40 Message: cvc-datatype-valid.1.2.1: '1971-05-16T00:00:00' is not a valid value for 'date'. ("time" not allowed) Error: Line number: 15 Column number: 40 Message: cvc-type.3.1.3: The value ' 1971-05-16T00:00:00 ' of element 'Date' is not valid. Error: Line number: 16 Column number: 32 Message: cvc-datatype-valid.1.2.1: '12-05-16' is not a valid value for 'date'. ("year" must have 4 digits or longer) Error: Line number: 16 Column number: 32 Message: cvc-type.3.1.3: The value ' 12-05-16 ' of element 'Date' is not valid. Error: Line number: 17 Column number: 28 Message: cvc-datatype-valid.1.2.1: '001234-05-31' is not a valid value for 'date'. ("year" with more than 4 digits can not have leading 0) Error: Line number: 17 Column number: 28 Message: cvc-type.3.1.3: The value '001234-05-31' of element 'Date' is not valid. Failed with errors: 8
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
Date/Time Seven-Property Model
"dateTime" Datatype Values and Representations
"dateTimeStamp" Datatype Values and Representations
►"date" Datatype Values and Representations
"time" Datatype Values and Representations
"gYear", "gMonth" and "gDay" Datatypes
"gYearMonth" and "gMonthDay" Datatypes
"duration" Datatype Values and Representations
"yearMonthDuration" Datatype Values and Representations
"dayTimeDuration" Datatype Values and Representations
Date, Time and Duration Datatype Summary
Miscellaneous Built-in Datatypes
Facets, Constraining Facets and Restriction Datatypes
"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