"yearMonthDuration" Datatype Values and Representations

This section describes the built-in primitive datatype, 'yearMonthDuration' that represents durations of time in units of months in Gregorian calendar. Leading and trailing whitespaces are allowed and trimmed. 'yearMonthDuration' values can be stored as 'integer' values.

If we look closer at the duration seven-property model, we actually consolidate "duYear", "duMonth", "duDay", "duHour", "duMinute" and "duSecond" into two properties:

Based on the consolidated duration model of "duSign", "duYearMonth" and "duDayTime", XSD introduced 2 new built-in datetypes: "yearMonthDuration" and "dayTimeDuration", so their values can be stored as single interger or decimal number.

Note that there is no easy way to consolidate all of them into a single property, because number of days in a month is not a constant in Gregorian calendar.

"yearMonthDuration" is a built-in datatype derived from "duration" datatype introduced in XSD 1.1 to represent a duration of time in units of months with these rules:

To verify these rules, I wrote this simple XSD document that uses "yearMonthDuration" datatype to declare XML elements:

<?xml version="1.1"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- yearMonthDuration_datatype_test.xsd
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<xs:element name="YearMonthDuration_Datatype_Test">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="YearMonthDuration" type="xs:yearMonthDuration"
         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"?>
<!-- yearMonthDuration_datatype_test.xml
 - Copyright (c) 2002-2013 HerongYang.com. All Rights Reserved.
-->
<YearMonthDuration_Datatype_Test>

<!-- 3 valid "YearMonthDuration" elements -->
  <YearMonthDuration>    P2147483647Y    </YearMonthDuration>
  <YearMonthDuration>   -P2147483647M    </YearMonthDuration>
  <YearMonthDuration>  P2147483647Y2147483647M  </YearMonthDuration>

<!-- 3 invalid "YearMonthDuration" elements -->
  <YearMonthDuration>    P2147483648Y    </YearMonthDuration>
  <YearMonthDuration>    P1D    </YearMonthDuration>
  <YearMonthDuration>    PT1H1M1S    </YearMonthDuration>
</YearMonthDuration_Datatype_Test>

Since "dateTimeStamp" is introduced in XSD 1.1, I need to use my Xsd11SchemaValidator.java program presented earlier in the book with Xerces2 XML Schema 1.1 JAR files to run the test:

herong> type java_xerces.bat
java -cp .;.\xerces-2_12_1-xml-schema-1.1\xml-apis.jar;
^^^.\xerces-2_12_1-xml-schema-1.1\xercesImpl.jar;
^^^.\xerces-2_12_1-xml-schema-1.1\serializer.jar;
^^^.\xerces-2_12_1-xml-schema-1.1\resolver.jar.jar;
^^^.\xerces-2_12_1-xml-schema-1.1\org.eclipse.wst.xml.xpath2.processor_1.2.0.jar;
^^^.\xerces-2_12_1-xml-schema-1.1\xercesSamples.jar; %1 %2 %3 %4 %5 %6

herong> java_java_xerces Xsd11SchemaValidator.java
^^^ yearMonthDuration_datatype_test.xsd
^^^ yearMonthDuration_datatype_test.xml

Error:
   Line number: 13
   Column number: 62
   Message: cvc-datatype-valid.1.2.1: 'P2147483648Y' is not a valid
   value for 'yearMonthDuration'. (Value out of "int" range)

Error:
   Line number: 13
   Column number: 62
   Message: cvc-type.3.1.3: The value '    P2147483648Y    ' of
   element 'YearMonthDuration' is not valid.

Error:
   Line number: 14
   Column number: 53
   Message: cvc-datatype-valid.1.2.1: 'P1D' is not a valid value for
   'yearMonthDuration'. (Days not allowed in "yearMonthDuration")

Error:
   Line number: 14
   Column number: 53
   Message: cvc-type.3.1.3: The value '    P1D    ' of element
   'YearMonthDuration' is not valid.

Error:
   Line number: 15
   Column number: 58
   Message: cvc-datatype-valid.1.2.1: 'PT1H1M1S' is not a valid value
   for 'yearMonthDuration'. (Hours, minutes and seconds not allowed)

Error:
   Line number: 15
   Column number: 58
   Message: cvc-type.3.1.3: The value '    PT1H1M1S    ' of element
   'YearMonthDuration' is not valid.

Failed with errors: 6

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