Functions That Work with Dates and Times

There is a very large emphasis in XPath 2.0 on functions that work with times and dates. The date and time functions that are designed to work on these XML schema types are as follows :

  • xs:dateTime accepts representations of the form "CCYY-MM-DDThh:mm:ss" where "CC" represents the century, "YY" the year, "MM" the month, and "DD" the day, preceded by an optional leading "" sign to indicate a negative number (if the sign is omitted, "+" is assumed). The letter "T" is the date/time separator and "hh", "mm", "ss" represent hour , minute, and second respectively. This may be immediately followed by a "Z" to indicate Coordinated Universal Time (UTC) or, to indicate the timezone (that is, the difference between the local time and Coordinated Universal Time, immediately followed by a sign, + or , followed by the difference from UTC represented as hh:mm). For example, 2004-08-31T14:30:00-05:00 represents 2:30 p.m. on August the 31st, 2004 for Eastern Standard Time, which is 5 hours behind Coordinated Universal Time.

  • xs:date accepts representations of the form "CCYY-MM-DD". An optional following timezone qualifier is permitted as for xs:dateTime .

  • xs:time accepts representations of the form "hh:mm:ss.sss" with an optional following timezone indicator.

  • xs:gYearMonth accepts representations that are reduced (right-truncated) from those allowed for xs:dateTime : "CCYY-MM". An optional following timezone qualifier is permitted.

  • xs:gYear accepts representations that are reduced (right-truncated) from those allowed for xs:dateTime : "CCYY". An optional following timezone qualifier is permitted as for xs:dateTime .

  • xs:gMonthDay accepts representations that are left-truncated from those accepted by xs:date : "--MM-DD". An optional following timezone qualifier is permitted as for xs:date .

  • xs:gMonth The lexical representation for gMonth is the left- and right-truncated lexical representation for xs:date : --MM--. An optional following timezone qualifier is permitted as for xs:date .

  • xs:gDay accepts representations that are left-truncated from those accepted by xs:date : "---DD". An optional following timezone qualifier is permitted as for xs:date .

    Two other data types that are also defined in XPath 2.0 in the xdt namespace, which corresponds to http://www.w3.org/2003/05/xpath-datatypes : xdt:yearMonthDuration and xdt:dayTimeDuration , both of which are types derived from xs:duration . Here's how you use them:

    MORE ON THE XML SCHEMA DATE-TIME TYPES

    You can find more information on the standard XML schema date/time types at http://www.w3.org/TR/xmlschema-2/#built-in-datatypes.


  • xdt:yearMonthDuration is derived from xs:duration by restricting it to contain only the year and month components . The reduced format looks like "PnYnM", where nY represents the number of years and nM the number of months. An optional preceding minus sign ("") is permitted to indicate a negative duration. If the sign is omitted a positive duration is indicated. For example, to indicate an xdt:yearMonthDuration of 2 years and 3 months, you would write: P2Y3M.

  • xdt:dayTimeDuration is derived from xs:duration by restricting it to contain only the day, hour, minute, and second components. That looks like "PnDTnHnMnS", where nD represents the number of days, T is the date/time separator, nH the number of hours, nM the number of minutes, and nS the number of seconds. An optional minus sign ("") is permitted to precede the "P", indicating a negative duration. For example, to indicate a duration of 5 days, 12 hours, and 22 minutes, you would write: P5DT12H22M.

SHOP TALK : THE TWO XDT DATA TYPES

When working with XPath, I sometimes feel that the two types derived from the xs:duration types, xdt:yearMonthDuration and xdt:dayTimeDuration , stick out rather awkwardly as add-on types to the XML-schema-defined types.

That makes me wonder what the story is here. Does the XPath 2.0 committee really want to keep these types separate from the XML schema specification?

It turns out that the answer may be nothe XML Query Working Group has recently requested the W3C XML Schema Working Group to include these two types, xdt:yearMonthDuration and xdt:dayTimeDuration , in the XML schema built-in data types.

It's not clear yet what the XML Schema Working Group will say to this request, but it's likely to be granted. In that case, these two data types will be removed from the xdt namespace and moved into the XML Schema namespace ( http://www.w3.org/2001/XMLSchema ) instead. Watch the XPath specifications for further details.


In XPath 2.0, you can use operators like =, >, <, and so on, on date/time values. For example, suppose that you wanted to determine whether an xs:dateTime was earlier or later than some other xs:dateTime . You could start by creating two variables , say $now and $then , and assigning $now the date 8/30/2004 and $then the date 8/31/2004:

 
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema">  <xsl:variable name="now" select="xs:dateTime('2004-08-30T10:20:00-05:00')" />   <xsl:variable name="then" select="xs:dateTime('2004-08-31T10:20:00-05:00')" />  .         .         . 

Now all you have to do is to compare the two xs:dateTime values using the > operator, as you see in ch11_02.xsl (Listing 11.2).

USING TIMEZONES

XML Schema date/time values may or may not include a timezone. This can create problems when you try to compare two values of the same type one of which has a timezone and the other does not. Such comparisons can in some cases be indeterminate. To avoid these problems, for comparisons and for some arithmetic functions, XPath 2.0 adds an implicit timezone from the evaluation context to date/time values that do not have a timezone. Note that the timezone is added only for the operator or function. The value itself is not changed.


Listing 11.2 Comparing Date-Time Values ( ch11_02.xsl )
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema">     <xsl:variable name="now" select="xs:dateTime('2004-08-30T10:20:00-05:00')" />     <xsl:variable name="then" select="xs:dateTime('2004-08-31T10:20:00-05:00')" />     <xsl:template match="/">  <xsl:value-of select="if($now > $then)   then 'Now is later than then.'   else 'Then is later than now.'"/>  </xsl:template> </xsl:stylesheet> 

And here's the result you get when you run this through Saxon:

 
 <?xml version="1.0" encoding="UTF-8"?> Then is later than now. 

You can also add and subtract time/date values using the + and - operators. Here is the list of op functions that show how + and - work on the various date/time data types (recall that op functions are not callable in XPath 2.0they just show what the behavior of the + and - operators is supposed to be):

  • op:subtract-dates returns the difference between two xs:date values as an xdt:dayTimeDuration value.

  • op:subtract-times returns the difference between two xs:time values as an xdt:dayTimeDuration value.

  • op:add-yearMonthDuration-to-dateTime adds an xdt:yearMonthDuration value to an xs:dateTime value.

  • op:add-dayTimeDuration-to-dateTime adds an xdt:dayTimeDuration value to an xs:dateTime value.

  • op:subtract-yearMonthDuration-from-dateTime subtracts an xdt:yearMonthDuration value from an xs:dateTime value.

  • op:subtract-dayTimeDuration-from-dateTime subtracts an xdt:dayTimeDuration value from an xs:dateTime value.

  • op:add-yearMonthDuration-to-date adds an xdt:yearMonthDuration value to an xs:date value.

  • op:add-dayTimeDuration-to-date adds an xdt:dayTimeDuration value to an xs:date value.

  • op:subtract-yearMonthDuration-from-date subtracts an xdt:yearMonthDuration value from an xs:date value.

  • op:subtract-dayTimeDuration-from-date subtracts an xdt:dayTimeDuration value from an xs:date value.

  • op:add-dayTimeDuration-to-time adds the value of the hour, minute, and second components of an xdt:dayTimeDuration value to an xs:time value.

  • op:subtract-dayTimeDuration-from-time subtracts the value of the hour, minute, and second components of an xdt:dayTimeDuration value from an xs:time value.

For example, when you subtract one xs:date time from another, you get an xdt:dayTimeDuration value. Here's an examplein this case, we're subtracting xs:date('2003-11-12') from xs:date('2004-10-06 ') to get an xdt:dayTimeDuration value corresponding to 325 days (this one's not in the code that accompanies the book, because Saxon doesn't support subtracting date/time values yet) :

 
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xs="http://www.w3.org/2001/XMLSchema">     <xsl:template match="/">  <xsl:value-of select="xs:date('2004-10-06') - xs:date('2003-11-12')"/>  </xsl:template> </xsl:stylesheet> 

Besides these operators, there are many functions designed to work on dates and times. We'll take a look at them now, starting with functions designed to extract values like hours or minutes from date/time values.



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net