XSLT and XPath function reference in alphabetical order

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 5, translated from German)

A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z

fn:max

Category:

Aggregation

Origin:

XPath 2.0

Return value:

A sequence consisting of an atomic item which has the maximum value of all items of the input sequence – after unification of their types.

Call/Arguments:

fn:max($inputSequence?, $collation-URI)

$inputSequence:

A sequence of one or more xs:anyAtomicType atomic items. If the input sequence does not completely consists of items of atomic type, the relevant items are atomised in advance. If the input sequence is empty, the empty sequence is returned.

$collation-URI:

Optional. The second argument consists of a xs:string string which names the URI string of the collation to be used. The value has to lexically correspond to the xs:anyURI type. If no second argument is passed on, the default collation is used for the string comparison.

Purpose of use:

The fn:max() function always returns a sequence of a sole item which has the highest value of all items of an input sequence passed on to the function. The type of the returned maximum value does not necessarily have to be equal to the type of the corresponding item of the input sequence since the return always occurs after a type unification. For example, a number of xs:float type of the input sequence may be returned as maximum value in the form of xs:double.

If several or all items have the same maximum value, only one of the relevant items is returned (which one exactly is dependent on the implementation).

If the input sequence is empty, the function returns the empty sequence. An error is not reported. But if exactly this is desired, the existence of a return value can be tested by means of the fn:exactly-one() function (see example 3).

The items of the input sequence have to be atomic and in a form (referred to as total order) which guarantees their comparability by means of the ge operator (»greater or equal«). Basically, »total order« means that it must be determinable for two items being recognised as unequal which of the two is greater or less than the other. A basic requirement is that all values have the same type, whereby also subtypes (derived types) of the appropriate types are permitted.

If the items of the input sequence are of xs:untypedAtomic type, they are converted to the number type xs:double.

If the equality of the types and thus the comparability is not guaranteed or cannot be achieved by type conversion, an appropriate error is outputted: »Invalid argument type« (err:FORG0006).

Comparison of numerical values:

The equality of the types of numerical values is also achieved by type promotion (conversion into a superordinated numeric type). If the input sequence contains one or more items of the NaN value, the function returns the NaN value.

Comparison of date and time values:

The comparability of values of the types xs:dateTime, xs:date or xs:time requires the fundamental presence or absence of a timezone indication. If such a value has not an appropriate indication, the implicit timezone indication is added resulting from the evaluation context. The date or time value is – if required – normalised on this basis and used in this form for the comparisons. In case (normalised) maximum values only differ by their timezone indication, it is decided by the implementation which of the values is returned.

Comparison of duration values:

Duration values to be compared have to be of xs:yearMonthDuration type or of xs:dayTimeDuration type.

Comparison of URI values and strings:

The comparison between xs:anyURI URI values and xs:string character strings is, after the conversion of items of the xs:anyURI type into xs:string, determined by means of a collation. For this purpose, the default collation or the collation determined by the second function argument is used.

In the simplest case the comparison of character strings is made character by character, whereby the following applies:

[z – a] > [Z – A] > [9 – 0]

The vaue passed on for the indication of the collation has to lexically correspond to the xs:anyURI type. If the collation is not supported, the error message »Unsupported collation« (err:FOCH0002) occurs.

In case a collation has been indicated, but the items of the input sequence (after a possible type conversion) do not have the xs:string type, the collation argument is ignored without the report of an error. This happens, for example, in case the input sequence consists of numeric types.

Example 1 – maximum of a numeric sequence:

fn:max((4, 5, 6, 7))

returns: 7 (of xs:integer type).

Example 2 – maximum of a string sequence:

fn:max(('bb','aa','AA'))

returns: 'bb' (of xs:string type).

Example 3 – catching the empty sequence:

fn:exactly-one(fn:max($data))

results in the error message: »fn:exactly-one called with a sequence containing zero or more than one item« in case the input sequence of fn:max() results in the empty sequence and the function returns the empty sequence.

In some cases the behaviour of fn:max() is unwanted with regard to an empty input sequence. In case, for example, an error message shall occur if no input data is available from which a maximum value can be determined, a solution would be to test the return value by means of the fn:exactly-one() function. If a maximum value is returned, it is passed on in a transparent way. However, in case of the empty sequence, an error occurs.

Function definition:

XPath 1.0:

The function is not available.

XPath 2.0:

fn:max($arg as xs:anyAtomicType*) as xs:anyAtomicType?

fn:max($arg as xs:anyAtomicType*, $collation as string) as xs:anyAtomicType?

   

<< back next >>

 

 

 


Copyright © Galileo Press, Bonn 2008
Printing of the online version is permitted exclusively for private use. Otherwise this chapter from the book "XSLT 2.0 & XPath 2.0" is subject to the same provisions as those applicable for the hardcover edition: The work including all its components is protected by copyright. All rights reserved, including reproduction, translation, microfilming as well as storage and processing in electronic systems.


Galileo Press, Rheinwerkallee 4, 53227 Bonn, Germany