"gYearMonth" and "gMonthDay" Datatypes

This section describes the built-in primitive datatype, 'gYearMonth' and 'gMonthDay', that represents year-month and month-day combinations in Gregorian calendar. Leading and trailing whitespaces are allowed and trimmed. Timezone offset is optional.

XSD 1.1 also provides 2 built-in datatypes, "gYearMonth" and "gMonthDay" to cover 2-property combination within a "date" value with these rules:

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

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- gYearMonth_gMonthDay_datatype_test.xsd
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<xs:element name="GYearMonth_GMonthDay_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="GYearMonth" type="xs:gYearMonth"
         maxOccurs="unbounded"/>
      <xs:element name="GMonthDay" type="xs:gMonthDay"
         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"?>
<!-- gYearMonth_gMonthDay_datatype_test.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<GYearMonth_GMonthDay_Datatype_Test>

<!-- 3 valid "GYearMonth" elements -->
  <GYearMonth>    0101-01    </GYearMonth>
  <GYearMonth>   -1234567-12Z   </GYearMonth>
  <GYearMonth>    2013-05-05:00    </GYearMonth>

<!-- 3 invalid "GYearMonth" elements -->
  <GYearMonth>    101-01    </GYearMonth>
  <GYearMonth>    2147483648-12Z    </GYearMonth>
  <GYearMonth>    2013Z    </GYearMonth>

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

<!-- 2 invalid "GMonthDay" elements -->
  <GMonthDay>   --1-1    </GMonthDay>
  <GMonthDay>   --13-05-05:00    </GMonthDay>
  <GMonthDay>   --12-05:00    </GMonthDay>
</GYearMonth_GMonthDay_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:

herong> java XsdSchemaValidator
^^^ gYearMonth_gMonthDay_datatype_test.xsd
^^^ gYearMonth_gMonthDay_datatype_test.xml

Error:
   Line number: 13
   Column number: 42
   Message: cvc-datatype-valid.1.2.1: '101-01' is not a valid value
   for 'gYearMonth'. ("Year" must be at leat 4 digits)

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

Error:
   Line number: 14
   Column number: 50
   Message: cvc-datatype-valid.1.2.1: '2147483648-12Z' is not a valid
   value for 'gYearMonth'. ("year" value out of "int" range")

Error:
   Line number: 14
   Column number: 50
   Message: cvc-type.3.1.3: The value '    2147483648-12Z    ' of
   element 'GYearMonth' is not valid.

Error:
   Line number: 15
   Column number: 41
   Message: cvc-datatype-valid.1.2.1: '2013Z' is not a valid value for
   'gYearMonth'. (Missing "month")

Error:
   Line number: 15
   Column number: 41
   Message: cvc-type.3.1.3: The value '    2013Z    ' of element
   'GYearMonth' is not valid.

Error:
   Line number: 22
   Column number: 38
   Message: cvc-datatype-valid.1.2.1: '--1-1' is not a valid value for
   'gMonthDay'. (2 digits needed for "month" and "day")

Error:
   Line number: 22
   Column number: 38
   Message: cvc-type.3.1.3: The value '   --1-1    ' of element
   'GMonthDay' is not valid.

Error:
   Line number: 23
   Column number: 46
   Message: cvc-datatype-valid.1.2.1: '--13-05-05:00' is not a valid
   value for 'gMonthDay'. ("month" value out of range)

Error:
   Line number: 23
   Column number: 46
   Message: cvc-type.3.1.3: The value '   --13-05-05:00    ' of
   element 'GMonthDay' is not valid.

Error:
   Line number: 24
   Column number: 43
   Message: cvc-datatype-valid.1.2.1: '--12-05:00' is not a valid
   value for 'gMonthDay'. (Missing "day" value)

Error:
   Line number: 24
   Column number: 43
   Message: cvc-type.3.1.3: The value '   --12-05:00    ' of element
   'GMonthDay' is not valid.

Failed with errors: 12

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 APIs

 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

 Archived Tutorials

 References

 Full Version in PDF/EPUB