XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (565 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
6.24Mb size Format: txt, pdf, ePub

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:

  • Days of the week are numbered from 1 (Monday) to 7 (Sunday).
  • Weeks of the year are numbered so that week 1 is the Monday-to-Sunday week that includes the first Thursday of the year. The days before week 1 fall in week 52 or 53 of the previous year.
  • ISO 8601 does not define a numbering for weeks within a month. You will have to see what your implementation returns.

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
.

Other books

Marked by Dean Murray
Persuasion Skills by Laurel Cremant
Flaw (The Flaw Series) by Ryan Ringbloom
Nina Coombs Pykare by The Dazzled Heart
The Passenger (Surviving the Dead) by James Cook, Joshua Guess
Small Medium at Large by Joanne Levy
Relentless: Three Novels by Lindsey Stiles
The Snow by Caroline B. Cooney
Club Destiny 1 Conviction by Nicole Edwards
Tainted by Temptation by Katy Madison