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

"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:

  • duYearMonth = duYear * 12 + duMonth
  • duDayTime = ( (duDay * 24 + duHour) * 60 + duMinute) * 60 + duSecond

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:

  • The value space of "yearMonthDuration" is all possible durations of time in units of months, by combining the "duYear" and "duDay" properties into days as "integer" values.
  • The lexical space of "yearMonthDuration" is all possible "yearMonthDuration" values represented in (duSign)P(duYear)?(duMonth)? pattern.
  • "duYear" and "duMonth" are both optional. But at least one of them must present.
  • Leading and trailing whitespaces allowed and trimmed.

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) 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) 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 to run the test:

c:\Progra~1\Java\jdk1.7.0_07\bin\java 
   -cp ".;c:\local\xerces-2_11_0-xml-schema-1.1-beta\xercesImpl.jar;
   c:\local\xerces-2_11_0-xml-schema-1.1-beta
   \org.eclipse.wst.xml.xpath2.processor_1.1.0.jar"
   Xsd11SchemaValidator 
   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

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

"yearMonthDuration" Datatype Values and Representations - Updated in 2014, by Dr. Herong Yang