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

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

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:

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