XSD Tutorials - Herong's Tutorial Examples - Version 5.10, by Dr. Herong Yang

"gYear", "gMonth" and "gDay" Datatypes

This section describes the built-in primitive datatype, 'gYear', 'gMonth' and 'gDay', that represents years, months and days in Gregorian calendar. Leading and trailing whitespaces are allowed and trimmed. Timezone offset is optional.

XSD 1.1 also provides 3 built-in datatypes, "gYear", "gMonth" and "gDay", to cover 3 properties within a "date" value with these rules:

  • The value space of "gYear" is all values of the "year" property representing all possible years in Gregorian calendar.
  • The lexical space of "gYear" is all possible "year" values represented in (year)(timezoneOffset)? patten.
  • The value space of "gMonth" is all values of the "month" property representing all possible months in a Gregorian calendar year.
  • The lexical space of "gMonth" is all possible "gMonth" values represented in --(month)(timezoneOffset)? patten.
  • The value space of "gDay" is all values of the "day" property representing all possible days in a Gregorian calendar month.
  • The lexical space of "gDay" is all possible "gDay" values represented in ---(day)(timezoneOffset)? patten.
  • "timezoneOffset" property is optional and no default timezone is defined.
  • Leading and trailing whitespaces allowed and trimmed.

To verify these rules, I wrote this simple XSD document that uses "gYear", "gMonth" and "gDay" datatypes to declare XML elements:

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- gYear_gMonth_gDay_datatype_test.xsd
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<xs:element name="GYear_GMonth_GDay_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="GYear" type="xs:gYear" 
         maxOccurs="unbounded"/>
      <xs:element name="GMonth" type="xs:gMonth"
         maxOccurs="unbounded"/>
      <xs:element name="GDay" type="xs:gDay"
         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"?>
<!-- gYear_gMonth_gDay_datatype_test.xml
 - Copyright (c) 2013, HerongYang.com, All Rights Reserved.
-->
<GYear_GMonth_GDay_Datatype_Test>

<!-- 3 valid "GYear" elements -->
  <GYear>    0101    </GYear>
  <GYear>   -1234567   </GYear>
  <GYear>    2013-05:00    </GYear>

<!-- 2 invalid "GYear" elements -->
  <GYear>    101    </GYear>
  <GYear>    2147483648    </GYear>

<!-- 2 valid "GMonth" elements -->
  <GMonth>   --01    </GMonth>
  <GMonth>   --12-05:00   </GMonth>

<!-- 2 invalid "GMonth" elements -->
  <GMonth>   --1    </GMonth>
  <GMonth>   --13    </GMonth>

<!-- 2 valid "GDay" elements -->
  <GDay>    ---01    </GDay>
  <GDay>    ---31-05:00   </GDay>

<!-- 2 invalid "GDay" elements -->
  <GDay>    ---1    </GDay>
  <GDay>    ---32    </GDay>
</GYear_GMonth_GDay_Datatype_Test>

When validating this XML document with my XsdSchemaValidator.java program presented earlier in the book, I get 6 groups of errors for 6 invalid XML elements:

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator 
   gYear_gMonth_gDay_datatype_test.xsd 
   gYear_gMonth_gDay_datatype_test.xml
   
Error:
   Line number: 13
   Column number: 29
   Message: cvc-datatype-valid.1.2.1: '101' is not a valid value for 
   'gYear'. (Minimum 4 digits needed)

Error:
   Line number: 13
   Column number: 29
   Message: cvc-type.3.1.3: The value '    101    ' of element 'GYear'
   is not valid.

Error:
   Line number: 14
   Column number: 36
   Message: cvc-datatype-valid.1.2.1: '2147483648' is not a valid 
   value for 'gYear'. (Value out of "int" range)

Error:
   Line number: 14
   Column number: 36
   Message: cvc-type.3.1.3: The value '    2147483648    ' of element
   'GYear' is not valid.

Error:
   Line number: 21
   Column number: 30
   Message: cvc-datatype-valid.1.2.1: '--1' is not a valid value for
   'gMonth'. (Minimum 2 digits needed)

Error:
   Line number: 21
   Column number: 30
   Message: cvc-type.3.1.3: The value '   --1    ' of element 'GMonth'
   is not valid.

Error:
   Line number: 22
   Column number: 31
   Message: cvc-datatype-valid.1.2.1: '--13' is not a valid value for
   'gMonth'. (Value out of "month" range)

Error:
   Line number: 22
   Column number: 31
   Message: cvc-type.3.1.3: The value '   --13    ' of element 
   'GMonth' is not valid.

Error:
   Line number: 29
   Column number: 28
   Message: cvc-datatype-valid.1.2.1: '---1' is not a valid value for
   'gDay'. (Minimum 2 digits needed)

Error:
   Line number: 29
   Column number: 28
   Message: cvc-type.3.1.3: The value '    ---1    ' of element 'GDay'
   is not valid.

Error:
   Line number: 30
   Column number: 29
   Message: cvc-datatype-valid.1.2.1: '---32' is not a valid value for
   'gDay'. (Value out of "day" range)

Error:
   Line number: 30
   Column number: 29
   Message: cvc-type.3.1.3: The value '    ---32    ' of element 
   'GDay' is not valid.

Failed with errors: 12

Last update: 2013.

Table of Contents

 About This Book

 Introduction to XML Schema

 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

 Using Xerces2 Java API

 XML Schema Language - Basics

 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

 Complex Element Declaration

 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

 Linking Multiple Schema Documents Together

 Glossary

 References

 PDF Printing Version

"gYear", "gMonth" and "gDay" Datatypes - Updated in 2014, by Dr. Herong Yang