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

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

You can manipulate these two duration subtypes using arithmetic operators and functions; for example, you can add and subtract two durations to give another duration, you can multiply or divide a duration by a number to get another duration, and you can divide one duration by another to get the ratio between the two durations as a number (more specifically, as an
xs:double
). You can also use functions such as
sum()
and
avg()
to get the total or average of a sequence of durations.

I personally prefer to use numbers for most of these operations. There's no reason why you can't use an
xs:double
to represent a duration in seconds, just as you would use one to represent a distance, a weight, a temperature or a voltage. Many calculations in fact become easier when you represent durations as numbers: for example, there is no way to divide a distance by a duration to obtain an average speed, except by converting the duration to a number. Similarly, if you want to work out how much to pay someone who has worked for five hours at $10/hour, it's no use multiplying the duration five hours by 10: the answer will be 2 days and 2 hours, not $50.

To convert a
dayTimeDuration
to a number of seconds, divide it by
xs:dayTimeDuration(‘PT1 S’)
. Similarly, to convert a number of seconds to a
dayTimeDuration
, multiply it by
xs:dayTimeDuration(‘PT1 S’).

Where the duration types do prove useful is when they are used in conjunction with dates and times. You can add a duration to a date or time to get another date or time, and you can subtract one date or time from another to get a duration.

Durations are written lexically in the notation defined by the ISO 8601 standard. The general form is the letter
P
, followed by one or more of the components
n
Y for the years,
n
M for the months,
n
D for days,
n
H for hours,
n
M for minutes, and
n
S for seconds. A
T
is used as a separator between the days and the hours. All the values are integers except for the seconds, which may be fractional. Zero components may be omitted (though at least one component must be present), and a negative duration may be written with a leading minus sign. So
P10Y6 M
is 10 years 6 months, while
PT10H30 M
is 10 hours, 30 minutes. XML Schema 1.0 treats the values
P12 M
and
P1Y
as distinct (an enumeration facet that permits one of these forms will not permit the other), but XPath treats them as equal, and will not retain any distinction between the two forms when converting the typed value back to a string value. XML Schema 1.1 follows XPath by normalizing the values.

Other books

Family Over Everything by Paige Green
Sword and Verse by Kathy MacMillan
Time of the Draig by Lisa Dawn Wadler
Montana Homecoming by Jillian Hart
Constantinou's Mistress by Cathy Williams
The Coldest Winter Ever by Sister Souljah
Out There by Simi Prasad