Functions That Extract Data from DateTime Values

Functions That Extract Data from Date/Time Values

There is an entire set of functions designed to let you work with date/time values by extracting data from those values:

  • fn:get- years -from-yearMonthDuration returns the year component of an xdt:yearMonthDuration value.

  • fn:get-months-from-yearMonthDuration returns the month component of an xdt:yearMonthDuration value.

  • fn:get-days-from-dayTimeDuration returns the day component of an xdt:dayTimeDuration value.

  • fn:get-hours-from-dayTimeDuration returns the hour component of an xdt:dayTimeDuration value.

  • fn:get-minutes-from-dayTimeDuration returns the minute component of an xdt:dayTimeDuration value.

  • fn:get-seconds-from-dayTimeDuration returns the second component of an xdt:dayTimeDuration value.

  • fn:get-year-from-dateTime returns the year component of an xs:dateTime value.

  • fn:get-month-from-dateTime returns the month component of an xs:dateTime value.

  • fn:get-day-from-dateTime returns the day component of an xs:dateTime value.

  • fn:get-hours-from-dateTime returns the hour component of an xs:dateTime value.

  • fn:get-minutes-from-dateTime returns the minute component of an xs:dateTime value.

  • fn:get-seconds-from-dateTime returns the second component of an xs:dateTime value.

  • fn:get-timezone-from-dateTime returns the timezone of an xs:dateTime value.

  • fn:get- year-from-date returns the year component of an xs:date value.

  • fn:get- month-from-date returns the month component of an xs:date value.

  • fn:get- day-from-date returns the day component of an xs:date value.

  • fn:get- timezone-from-date returns the timezone of an xs:date value.

  • fn:get- hours-from-time returns the hour component of an xs:time value.

  • fn:get- minutes-from-time returns the minute component of an xs:time value.

  • fn:get- seconds-from-time returns the second component of an xs:time value.

  • fn:get-timezone-from-time returns the timezone of an xs:time value.

Each of these functions has its own syntax and usage, so we'll take a look at them briefly .

The fn:get-years-from-yearMonthDuration Function

This function lets you extract the year component of an xdt:yearMonthDuration value. Here's how it works:

 
 fn:get-years-from-yearMonthDuration(  $srcval  as xdt:yearMonthDuration?)     as xs:integer? 

This function returns an xs:integer representing the value of the year component of $srcval , which may be negative.

You can see an example in ch11_03.xsl (Listing 11.3), where we're extracting the years from the duration "P20Y03M" (20 years, 3 months). Note that to use xdt:yearMonthDuration values, we have to add an xmlns:xdt attribute to declare the xdt namespace: "http://www.w3.org/2003/05/xpath-datatypes".

Listing 11.3 Using fn:get-years-from-yearMonthDuration ( ch11_03.xsl )
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  xmlns:xdt="http://www.w3.org/2003/05/xpath-datatypes"  xmlns:xs="http://www.w3.org/2001/XMLSchema">     <xsl:template match="/">  <xsl:value-of select="get-years-from-yearMonthDuration(   xdt:yearMonthDuration('P20Y03M'))"/>  </xsl:template> </xsl:stylesheet> 

As you'd expect, this is the result when you use this example in Saxon:

 
 <?xml version="1.0" encoding="UTF-8"?> 20 

The fn:get-months-from-yearMonthDuration Function

This function just lets you extract the value of the month component of an xdt:yearMonthDuration value. Here's how you use it:

 
 fn:get-months-from-yearMonthDuration(  $srcval  as xdt:yearMonthDuration?) as xs:integer? 

This function returns an xs:integer representing the month component of $srcval , which may be negative. For example, fn:get-months-from-yearMonthDuration(xdt:yearMonthDuration("P20Y03M")) returns 3.

The fn:get-days-from-dayTimeDuration Function

The fn:get-days-from-dayTimeDuration function lets you extract the value of the day component from an xdt:dayTimeDuration value. Here's how you use it:

 
 fn:get-days-from-dayTimeDuration(  $srcval  as xdt:dayTimeDuration?)     as xs:integer? 

This function returns an xs:integer representing the value of the day component of $srcval , which may be negative. You can see an example in ch11_04.xsl (Listing 11.4), where we're extracting the number of days from the xs:dayTimeDuration value "P5DT11H" (5 days, 11 hours).

Listing 11.4 Using fn:get-days-from-dayTimeDuration ( ch11_04.xsl )
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xdt="http://www.w3.org/2003/05/xpath-datatypes"     xmlns:xs="http://www.w3.org/2001/XMLSchema">     <xsl:template match="/">  <xsl:value-of select="get-days-from-dayTimeDuration(   xdt:dayTimeDuration('P5DT11H'))"/>  </xsl:template> </xsl:stylesheet> 

And here's the result5 days:

 
 <?xml version="1.0" encoding="UTF-8"?> 5 

The fn:get-hours-from-dayTimeDuration Function

The fn:get-hours-from-dayTimeDuration function lets you extract the value of the hour component of an xdt:dayTimeDuration value. Here's how you use it:

 
 fn:get-hours-from-dayTimeDuration(  $srcval  as xdt:dayTimeDuration?)     as xs:integer? 

This function returns an xs:integer containing the value of the hour component of $srcval , which may be negative. Here's an example:

 
 fn:get-hours-from-dayTimeDuration(xdt:dayTimeDuration("P5DT11H")) returns 11. 

The fn:get-minutes-from-dayTimeDuration Function

This function lets you extract the value of the minute component from an xdt:dayTimeDuration value. Here's how you use it:

 
 fn:get-minutes-from-dayTimeDuration(  $srcval  as xdt:dayTimeDuration?)     as xs:integer? 

This function returns an xs:integer representing the minute component of $srcval , which may be negative. You can see an example in ch11_05.xsl (Listing 11.5), where we're extracting the minutes from the xdt:dayTimeDuration value "P3DT11H22M".

Listing 11.5 Using fn:get-minutes-from-dayTimeDuration ( ch11_05.xsl )
 <xsl:stylesheet version="2.0"     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"     xmlns:xdt="http://www.w3.org/2003/05/xpath-datatypes"     xmlns:xs="http://www.w3.org/2001/XMLSchema">     <xsl:template match="/">  <xsl:value-of select="get-minutes-from-dayTimeDuration(   xdt:dayTimeDuration('P3DT11H22M'))"/>  </xsl:template> </xsl:stylesheet> 

And here's the result when you use Saxon22 minutes:

 
 <?xml version="1.0" encoding="UTF-8"?> 22 

The fn:get-seconds-from-dayTimeDuration Function

As you can guess from its name , this function extracts the value of the second component from an xdt:dayTimeDuration value. Here's the signature for this function:

 
 fn:get-seconds-from-dayTimeDuration( $srcval  as xdt:dayTimeDuration?)     as xs:decimal? 

This function just returns an xs:decimal value representing the value of the second component of $srcval , which may be negative. Here's an example:

 
 fn:get-seconds-from-dayTimeDuration(xdt:dayTimeDuration("P5DT11H23.4S")) returns 23.4. 

The fn:get-year-from-dateTime Function

This function extracts the year from xs:dateTime values. Here's its signature:

 
 fn:get-year-from-dateTime(  $srcval  as xs:dateTime?) as xs:integer? 

This function returns an xs:integer representing the value of the year component of $srcval , which may be negative.

Here's an example where we're checking the year of someone's birthday. In this case, we'll check if that year is before or after 1900, as you can see in ch11_06.xsl (Listing 11.6).

Listing 11.6 Using fn:get-year-from-dateTime ( ch11_06.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="birthday"   select="xs:dateTime('1899-07-04T19:21:00-05:00')" />  <xsl:template match="/">  <xsl:value-of select="if (get-year-from-dateTime($birthday) lt 1900)   then 'Getting kind of old, eh?'   else 'Happy Birthday!'"/>  </xsl:template> </xsl:stylesheet> 

And here's the result:

 
 <?xml version="1.0" encoding="UTF-8"?> Getting kind of old, eh? 

The fn:get-month-from-dateTime Function

This function just extracts the month from xs:dateTime values, and here's how you use it:

 
 fn:get-month-from-dateTime(  $srcval  as xs:dateTime?) as xs:integer? 

This function returns an xs:integer between 1 and 12, inclusive, representing the value of the month component of $srcval .

Here's an example:

 
 fn:get-month-from-dateTime(xs:dateTime("2004-07-11T19:21:00-05:00")) returns 7. 

The fn:get-day-from-dateTime Function

This function returns the value of the day component from an xs:dateTime value, like this:

 
 fn:get-day-from-dateTime(  $srcval  as xs:dateTime?) as xs:integer? 

When called, this function returns an xs:integer between 1 and 31, inclusive, representing the value of the day component of $srcval . Here's an example:

 
 fn:get-day-from-dateTime(xs:dateTime("2004-05-31T13:20:00-05:00")) returns 31. 

The fn:get-hours-from-dateTime Function

The fn:get-hours-from-dateTime function extracts the value of the hour component from xs:dateTime values, and here's what this function looks like:

 
 fn:get-hours-from-dateTime(  $srcval  as xs:dateTime?) as xs:integer? 

Here, this function returns an xs:integer between 0 and 23, inclusive, representing the value of the hour component of $srcval . And here's an example:

 
 fn:get-hours-from-dateTime(xs:dateTime("2004-11-09T12:00:00")) returns 12. 

The fn:get-minutes-from-dateTime Function

As you can guess from its name, the fn:get-minutes-from-dateTime function extracts the value of the minute component from an xs:dateTime value, like this:

 
 fn:get-minutes-from-dateTime(  $srcval  as xs:dateTime?) as xs:integer? 

This function returns an xs:integer value between 0 and 59, inclusive, containing the value of the minute component of $srcval .

The fn:get-seconds-from-dateTime Function

The fn:get-seconds-from-dateTime function extracts the value of the second component from xs:dateTime values, like this:

 
 fn:get-seconds-from-dateTime(  $srcval  as xs:dateTime?) as xs:decimal? 

When called, this function returns an xs:decimal value between 0 and 60.999..., inclusive, corresponding to the value of the second component in $srcval (the value can be greater than 60 seconds to allow for leap seconds). Here's an example:

 
 fn:get-seconds-from-dateTime(xs:dateTime("2004-08-19T11:30:29-05:00")) returns 29. 

The fn:get-timezone-from-dateTime Function

This function lets you determine the timezone of an xs:dateTime value, and here's how you use it:

 
 fn:get-timezone-from-dateTime($srcval as xs:dateTime?) as xdt:dayTimeDuration? 

The fn:get-timezone-from-dateTime function returns the timezone component of $srcval as an xdt:dayTimeDuration value indicating the difference from UTC (its value may range from +14:00 to 14:00 hours, inclusive).

For example, this expression returns an xdt:dayTimeDuration value of "-PT5H":

 
 fn:get-timezone-from-dateTime(xs:dateTime("2004-08-19T11:30:29-05:00")) 

The fn:get-year-from-date Function

As you can gather from this function's name, it extracts the year from an xs:date value. Here's how you use this function:

 
 fn:get-year-from-date(  $srcval  as xs:date?) as xs:integer? 

This function returns an xs:integer value containing the value of the year component of $srcval , which may be negative. For example, fn:get-year-from-date(xs:date("2004-09-01")) returns 2004.

The fn:get-month-from-date Function

The fn:get-month-from-date function just gets the month from xs:date values, and here's how you use it:

 
 fn:get-month-from-date(  $srcval  as xs:date?) as xs:integer? 

This function returns an xs:integer between 1 and 12, inclusive, containing the value of the month component of $srcval . For example, fn:get-month-from-date(xs:date("2004-03-11+05:00")) returns 3.

The fn:get-day-from-date Function

This function returns the day component of xs:date values:

 
 fn:get-day-from-date($srcval as xs:date?) as xs:integer? 

The value returned is an xs:integer between 1 and 31, inclusive, containing the day component of $srcval . For example, fn:get-day-from-date(xs:date("2004-03-11+05:00")) returns 11.

The fn:get-timezone-from-date Function

This function extracts the timezone from xs:date values:

 
 fn:get-timezone-from-date(  $srcval  as xs:date?) as xdt:dayTimeDuration? 

The value returned is the timezone component of $srcval as an xdt:dayTimeDuration value that indicates the difference from UTC (its value may range from +14:00 to 14:00 hours, inclusive). For example, fn:get-timezone-from-date(xs:date("2004-03-11+05:00")) returns an xdt:dayTimeDuration value of "-PT5H". The expression fn:get-timezone-from-date(xs:date("2004-09-12Z")) returns the xdt:dayTimeDuration value of PT0H.

The fn:get-hours-from-time Function

This function retrieves the value of the hour component of an xs:time value:

 
 fn:get-hours-from-time(  $srcval  as xs:time?) as xs:integer? 

The returned value is an xs:integer between 0 and 23, inclusive, representing the value of the hour component of $srcval . For example, fn:get-hours-from-time(xs:time("10:37:00")) returns 10 and fn:get-hours-from-time(xs:time("02:19:27+05:00")) returns 21.

The fn:get-minutes-from-time Function

The fn:get-minutes-from-time function returns the minute component from an xs:time value:

 
 fn:get-minutes-from-time(  $srcval  as xs:time?) as xs:integer? 

The return value is an xs:integer value between 0 to 59, inclusive, containing the value of the minute component of $srcval . For example, fn:get-minutes-from-time(xs:time("14:23:19")) returns 23.

The fn:get-seconds-from-time Function

This function returns the second component from an xs:time value:

 
 fn:get-seconds-from-time(  $srcval  as xs:time?) as xs:decimal? 

The returned value is an xs:decimal value between 0 and 60.999..., inclusive. For example, fn:get-seconds-from-time(xs:time("13:20:10.5")) returns 10.5.

The fn:get-timezone-from-time Function

The fn:get-timezone-from-time function is the last of the date/time extraction functions, and it extracts the timezone from an xs:time value. Here's how you use it:

 
 fn:get-timezone-from-time(  $srcval  as xs:time?) as xdt:dayTimeDuration? 

When called, this function returns an xdt:dayTimeDuration value containing the timezone component of $srcval as an xdt:dayTimeDuration value. The returned value indicates the difference of the timezone of $srcval from UTC (this value may range from +14:00 to 14:00 hours, inclusive).

For example, fn:get-timezone-from-time(xs:time("19:48:00-05:00")) returns an xdt:dayTimeDuration value of "-PT5H".



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