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

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

See Also

Regular Expression Syntax, Chapter 14

contains()
on page 730

ends-with()
on page 773

replace()
on page 862

starts-with()
on page 875

tokenize()
on page 894

max, min

The
max()
and
min()
functions returns the maximum or minimum value in a sequence. The input sequence may contain any items that can be compared using the
lt
and
gt
operators.

Signature

Argument
Type
Meaning
sequence
xs:anyAtomicType*
The input sequence
collation
(optional)
xs:string
Collation used for comparing strings
Result
xs:anyAtomicType?
The maximum/minimum value found in the input sequence

Effect

If the sequence supplied in the function call contains nodes, then the nodes will automatically be atomized (to extract their typed values) as part of the function call mechanism.

Any untyped atomic values in the atomized sequence (which will typically result from atomizing a node in a schema-less document) are converted to
xs:double
values. A runtime error is reported if there are values that cannot be converted. If there are NaN (not-a-number) values in the sequence, which might happen if you do the conversion to numbers yourself using the
number()
function, then the result of the function is NaN. If the input sequence is empty, the result is an empty sequence.

If the sequence contains numeric values of different types, then they are all converted to the
least common type
. This means that if the sequence contains an
xs:double
, all the values will be converted to
xs:double
; otherwise, if it contains an
xs:float
, all the values will be converted to
xs:float
. Similarly, if the sequence contains a mixture of
xs:string
and
xs:anyURI
values, then the
xs:anyURI
values will be converted to
xs:string
.

In the resulting sequence, all the values must be comparable using the
lt
operator. This rules out values of types such as
xs:QName
and
xs:hexBinary
for which no ordering is defined, and it rules out sequences that mix values such as integers and strings. The
max()
function then returns a value that is greater than or equal to every other value in the sequence, while the
min()
function returns a value that is less than or equal to every other value in the sequence. This will always be a value after any conversion: for example
max((10, 1.5e0))
is the
xs:double
value 10e0.

If there are two values that both satisfy this condition (for example two
xs:dateTime
values in different timezones) then it is not predictable which of them will be returned.

If the
collation
argument is supplied, then it is used when comparing strings. If the sequence contains strings and no
collation
is supplied, then the default collation is used.

Examples

Expression
Result
max((10, 20, xs:float(–5), 13))
20
as an
xs:float
value
max((“a”, “x”, “b”))
“x”
(assuming a typical collation)
max(2)
2
max(())
()
min((xs:dayTimeDuration(‘PT10 S’), xs:dayTimeDuration(‘PT1 M’)))
PT10 S as an
xs:dayTimeDuration
min((xs:date(‘2000-01-01’), current-date()))
2000-01-01 as an
xs:date

Usage

Note that
max()
and
min()
return an atomic value. If you supply a sequence of nodes, the nodes are atomized, and the highest (or lowest) atomic value is returned. If you actually want to know which node contained the highest or lowest value, you will have to search for it, using a predicate. For example:

$nodes[size=max($nodes/size)]

Because of this limitation, it may sometimes be better to use the technique of sorting the nodes and selecting the first or last. For example:


  

  

    

  


A common usage is to find the larger or smaller of two values. Remember in this case that you need to construct a sequence containing the two values:
max((0, $amount-due))
. If you forget the second pair of parentheses, you are calling the two-argument form of the function, which is likely to result in the rather cryptic message “Unknown collation”.

Other books

Watchers of the Dark by Biggle Jr., Lloyd
Heatseeker (Atrati) by Monroe, Lucy
Skios: A Novel by Frayn, Michael
Destroying Angel by Sam Hastings
A Fortunate Man by John Berger
Lucian: Dark God's Homecoming by Van Allen Plexico
City of Boys by Beth Nugent
Three Kings for Sarah by Noa Xireau