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

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

Here are some examples:

Expression
Result
xs:dayTimeDuration(“P10D”) div xs:dayTimeDuration(“PT6H”)
40
xs:dayTimeDuration(“-P1D”) div xs:dayTimeDuration(“PT1S”)
−86400
xs:yearMonthDuration(“P1M”) div xs:yearMonthDuration(“P1Y”)
0.083333333…

This operation provides the easiest way to convert a duration into a number of months, days, or seconds. To convert an
xs:dayTimeDuration
to seconds, for example, just divide it by
xs:dayTimeDuration(“PT1S”)
. This is useful when you need to perform calculations that are not directly supported by the operations available on durations. Examples include:

  • Dividing a distance by a duration to obtain an average speed.
  • Multiplying the number of hours worked by the hourly rate to obtain the amount of money due.
  • Determining the day of the week for a given date.

The following code illustrates how to display the day of the week, supplied in the variable
$date
:

(“Sunday”, “Monday”, “Tuesday”, “Wednesday”, “Thursday”, “Friday”, “Saturday”)

   [1 + (($date - xs:date(“1901-01-06”)) div xs:dayTimeDuration(“P1D”) mod 7)]

In XSLT 2.0, however, you can display the day of the week using the
format-date()
function, described in Chapter 13.

Value Comparisons

XPath 2.0 has introduced a completely new set of operators for comparing single atomic values. These are shown in the table below.

Operator
Meaning
eq
equals
ne
not equals
lt
less than
le
less than or equal to
gt
greater than
ge
greater than or equal to

These were introduced primarily because they have much cleaner and more predictable behavior than the XPath 1.0 operators
=
,
!=
,
<
,
<=
,
>
, and
>=
. The XPath 1.0 operators are still available, and they are described later in this chapter under the heading
General Comparisons
on page 588.

Other books

Chasing Venus by Diana Dempsey
Lords of Corruption by Kyle Mills
The Fish Kisser by James Hawkins
All Hell by Allan Burd
Duncton Found by William Horwood