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

"duration" Datatype Values and Representations

This section describes the built-in primitive datatype, 'duration' that represents durations of time. Leading and trailing whitespaces are allowed and trimmed. A 'duration' lexical representation roughly follows this format: PiYiMiDTiHiMdS.

Similar to the data/time seven-property model, XSD 1.1 uses a duration seven-property model to represent a duration of time in the Gregorian calendar system. This model is closely related to "ISO 8601 - Data elements and interchange formats" standard.

In this model, any duration of time can be represented with a set of 6 values named as:

  • "duSign" - A positive "int" value of 1 or -1 representing the sign of the duration.
  • "duYear" - A positive "int" value representing number of years in the duration.
  • "duMonth" - A positive "int" value representing number of months in the duration.
  • "duDay" - A positive "int" value representing number of days in the duration.
  • "duHour" - A positive "int" value representing number of hours in the duration.
  • "duMinute" - A positive "int" value representing number of minutes in the duration.
  • "duSecond" - A positive "decimal" value representing number of seconds and fractions of a second of in the duration.

Each property in the seven-property model has its own lexical space:

  • "duSign" lexical space is blank or "-". "+" is not allowed.
  • "duYear" lexical space is all "duYear" values expressed in "int" lexical presentations followed by "Y".
  • "duMonth" lexical space is all "duMonth" values expressed in "int" lexical presentations followed by "M".
  • "duDay" lexical space is all "duDay" values expressed in "int" lexical presentations followed by "D".
  • "duHour" lexical space is all "duHour" values expressed in "int" lexical presentations followed by "H".
  • "duMinute" lexical space is all "duMinute" values expressed in "int" lexical presentations followed by "M".
  • "duSecond" lexical space is all "duSecond" values expressed in "decimal" lexical presentations followed by "S".

With this duration seven-property model, the "duration" built-in datatype can be easily described as below.

"duration" is a built-in datatype that uses the duration seven-property model to represent a duration of time with these rules:

  • The value space of "duration" is all possible durations of time in the Gregorian calendar.
  • The lexical space of "duration" is all possible "duration" values represented in (duSign)P(duYear)?(duMonth)?(duDay)?(T(duHour)?(duMinute)?(duSecond)?)? pattern.
  • A "duration" lexical representation roughly follows this format: PiYiMiDTiHiMdS.
  • "duYear", "duMonth", "duDay", "duHour", "duMinute" and "duSecond" are all 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 "time" datatype to declare XML elements:

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

<!-- 6 valid "Duration" elements -->
  <Duration>    P2147483647Y    </Duration>
  <Duration>   -P2147483647M    </Duration>
  <Duration>    P2147483647D    </Duration>
  <Duration>   -PT2147483647H    </Duration>
  <Duration>    PT2147483647M    </Duration>
  <Duration>   -PT123456789012345.123456789012345S</Duration>
  <Duration>
P2147483647Y2147483647M2147483647DT2147483647H2147483647M123456789012S
  </Duration>

<!-- 5 invalid "Duration" elements -->
  <Duration>    P2147483648Y    </Duration>
  <Duration>    P2147483648D    </Duration>
  <Duration>    PT2147483648H    </Duration>
  <Duration>    P1D1M1Y    </Duration>
  <Duration>    P1H1M1S    </Duration>
</Duration_Datatype_Test>

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

c:\Progra~1\Java\jdk1.7.0_07\bin\java XsdSchemaValidator 
   duration_datatype_test.xsd duration_datatype_test.xml
   
Error:
   Line number: 19
   Column number: 44
   Message: cvc-datatype-valid.1.2.1: 'P2147483648Y' is not a valid 
   value for 'duration'. (Value is out of "int" range)

Error:
   Line number: 19
   Column number: 44
   Message: cvc-type.3.1.3: The value '    P2147483648Y    ' of 
   element 'Duration' is not valid.

Error:
   Line number: 20
   Column number: 44
   Message: cvc-datatype-valid.1.2.1: 'P2147483648D' is not a valid 
   value for 'duration'. (Value is out of "int" range)

Error:
   Line number: 20
   Column number: 44
   Message: cvc-type.3.1.3: The value '    P2147483648D    ' of 
   element 'Duration' is not valid.

Error:
   Line number: 21
   Column number: 45
   Message: cvc-datatype-valid.1.2.1: 'PT2147483648H' is not a valid
   value for 'duration'. (Value is out of "int" range)

Error:
   Line number: 21
   Column number: 45
   Message: cvc-type.3.1.3: The value '    PT2147483648H    ' of 
   element 'Duration' is not valid.

Error:
   Line number: 22
   Column number: 39
   Message: cvc-datatype-valid.1.2.1: 'P1D1M1Y' is not a valid value 
   for 'duration'. (Wrong sequence)

Error:
   Line number: 22
   Column number: 39
   Message: cvc-type.3.1.3: The value '    P1D1M1Y    ' of element 
   'Duration' is not valid.

Error:
   Line number: 23
   Column number: 39
   Message: cvc-datatype-valid.1.2.1: 'P1H1M1S' is not a valid value 
   for 'duration'. (Missing "T")

Error:
   Line number: 23
   Column number: 39
   Message: cvc-type.3.1.3: The value '    P1H1M1S    ' of element 
   'Duration' is not valid.

Failed with errors: 10

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

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