Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
A very common use of these functions is simply to output the current date; for example,
{
format-dateTime(current-dateTime(), …)
. In this case you don't have to worry about whether the supplied date is in the correct ISO format and calendar: it always will be.
If the
calendar
argument selects a calendar other than the Gregorian calendar, then the date is translated into that calendar before extracting the relevant component. Not all calendars include concepts directly equivalent to months and weeks (for example, some calendars use lunar months), but most have concepts that are sufficiently similar for this to work. In practice, the handling of non-Gregorian calendars is likely to vary depending on your implementation and is only very loosely described in the XSLT specification.
The numbering of days of the week and weeks of the year may vary from one country or language to another. If you want predictable results, select the
ISO
calendar in the
calendar
argument. The results will then follow the rules in ISO 8601:
It's important to understand the distinction in XML Schema between the value space of a type and the lexical space. The value space for
xs:date
simply contains one data point for every day in the history of the world, past, present, and future (within limits, but they need not concern us). It's not meaningful to ask whether these data points are represented as integers or strings or to ask what format they are in—or to get to the point, it's not meaningful to ask what calendar they are in. There is a data point representing the day when the Great Fire of London started, and you can represent this data point using any calendar you like. So the value space of dates in XML Schema is calendar neutral.
The same is not true of the lexical space. The lexical representation of dates in XML Schema uses the Gregorian calendar. In fact, it uses the Gregorian calendar even to represent dates that occurred long before the Gregorian calendar was invented: this is referred to as the
proleptic
Gregorian calendar (ISO 8601 calls it “prolaptic,” but that appears to be an error). This simply projects the Gregorian calendar backward in time. This is really no different from our use of “B.C.” dates: we are using a representation of dates that is unrelated to the way those same dates were represented in contemporary records.
This means that if you want to use a non-Gregorian calendar, you have to be very careful. For example, if you are storing a historical document that records that the Great Fire of London broke out on September 2, 1666, then if you want to represent this correctly using the
xs:date
type you need to know how to convert it into a Gregorian date. In fact, the correct lexical representation of this date is
1666-09-12
, as there was then a 10-day difference between the Julian and Gregorian calendars. (Although the Gregorian calendar was introduced in 1585, Britain has always been slow to pick up European ideas, and did not adopt it until 1752.)
If you now want to format this date using the Julian calendar, you can do so by specifying
OS
in the
calendar
argument. This will produce the correct output (
Sunday 2nd Sept 1666
) only if you represented the date correctly in the value space. If you were careless, and created the date by writing
xs:date(“1666-09-02”)
, then the formatted output will be
Thursday 23rd Aug 1666
.