Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
XPath provides no functions to format durations in a user-friendly output representation. Instead there are six functions
years-from-duration()
,
months-from-duration()
,
days-from-duration()
,
hours-from-duration()
,
minutes-from-duration()
, and
seconds-from-duration()
, which allow the components to be extracted. These will be the components after normalizing the value: for example if the duration is supplied as
P18 M
, then extracting the components will give you one year and six months. If the duration is negative, then all the components will be supplied as negative numbers.
The Minor Atomic Types
The previous section covered the major types of XPath 2.0, the ones that are well supported by functions and operators in the language: specifically,
xs:anyURI
,
xs:boolean
,
xs:date
,
xs:dateTime
,
xs:decimal
,
xs:double
,
xs:integer
,
xs:QName
,
xs:string
,
xs:time
,
xs:dayTimeDuration
, and
xs:yearMonthDuration
.
In this section I will briefly survey what I call the minor atomic types. These are defined as primitive types in XML Schema, but they are not well supported by XPath, and you are unlikely to use them very often. These are
xs:gYear
,
xs:gYearMonth
,
xs:gMonth
,
xs:gMonthDay
,
xs:gDay
,
xs:duration
,
xs:float
,
xs:hexBinary
,
xs:base64Binary
, and
xs:NOTATION
.
The Partial Date Types
This category refers to the five types
xs:gYear
,
xs:gYearMonth
,
xs:gMonth
,
xs:gMonthDay
, and
xs:gDay
. They essentially represent dates in which one or two of the components are missing.
It has to be said that these types have been treated with a certain amount of derision by commentators. I have heard them referred to as the
gHorribleKludge
types, or (after the pronunciation of “gDay”), the
Strine
types. I have rarely seen them used in real applications, and it does seem fairly extraordinary that these types, even if someone finds them useful, should be considered as primitive types on the same level as string, boolean, and double. For my part, if I want to design an XML database that includes information about the vintage years of my favorite wines, I think I can do it without using the
xs:gYear
type, let alone an
xs:gYear
with a timezone.
But for better or worse, they are there—so we might as well describe them and move on.
The lexical representation of these values follows ISO 8601 conventions, using hyphens to represent missing components. ISO 8601 does not allow timezones on these values; this is an extra addition by the XML Schema working group. The allowed formats are shown using examples in the table below.
Type | Without timezone | With timezone |
xs:gYear | 2008 | 2008+08:00 |
xs:gYearMonth | 2008-07 | 2008-07+08:00 |
xs:gMonth | --07 | --07+08:00 |
xs:gMonthDay | --07-31 | --07-31+08:00 |
xs:gDay | ---31 | ---31+08:00 |
For the
xs:gYear
and
xs:gYearMonth
types, an optional leading minus sign is allowed to indicate BC dates.
The format of
xs:gMonth
values was shown incorrectly in the original XML Schema Recommendation as
--MM--
. The error was corrected in a subsequent erratum, but in the meantime it has found its way into many books on XML Schema and a number of software products.
XPath 2.0 allows conversion of these values to and from strings. It allows them to be compared with each other using the
=
and
!=
operators, but they cannot be sorted or compared using
<
and
>
. Comparison uses the implicit timezone if the value itself has no timezone. This means that two
xs:gYear
values are not equal to each other if they are in different timezones. (If you ever come across an application that relies on this, let me know.)