fn:max

Returns a value that is equal to the highest value appearing in the input sequence.

Signatures

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

Properties

The zero-argument form of this function is deterministic, context-dependent, and focus-independent. It depends on collations, and implicit timezone.

The one-argument form of this function is deterministic, context-dependent, and focus-independent. It depends on collations, and static base URI, and implicit timezone.

Rules

The following conversions are applied to the input sequence $arg, in order:

  1. Values of type xs:untypedAtomic in $arg are cast to xs:double.

  2. If the resulting sequence contains values that are instances of more than one primitive type (meaning the 19 primitive types defined in [Schema 1.1 Part 2]), then:

    1. If each value is an instance of one of the types xs:string or xs:anyURI, then all the values are cast to type xs:string.

    2. If each value is an instance of one of the types xs:decimal or xs:float, then all the values are cast to type xs:float.

    3. If each value is an instance of one of the types xs:decimal, xs:float, or xs:double, then all the values are cast to type xs:double.

    4. Otherwise, a type error is raised [ERRFORG0006].

    The primitive type of an xs:integer value for this purpose is xs:decimal.

The items in the resulting sequence may be reordered in an arbitrary order. The resulting sequence is referred to below as the converted sequence. The function returns an item from the converted sequence rather than the input sequence.

If the converted sequence is empty, the function returns the empty sequence.

All items in the converted sequence must be derived from a single base type for which the le operator is defined. In addition, the values in the sequence must have a total order. If date/time values do not have a timezone, they are considered to have the implicit timezone provided by the dynamic context for the purpose of comparison. Duration values must either all be xs:yearMonthDuration values or must all be xs:dayTimeDuration values.

If the converted sequence contains the value NaN, the value NaN is returned (as an xs:float or xs:double as appropriate).

If the items in the converted sequence are of type xs:string or types derived by restriction from xs:string, then the determination of the item with the smallest value is made according to the collation that is used. If the type of the items in the converted sequence is not xs:string and $collation is specified, the collation is ignored.

The collation used by this function is determined according to the rules in Choosing a collation.

The function returns the result of the expression:

   if (every $v in $c satisfies $c[1] ge $v)
   then $c[1] 
   else fn:max(fn:tail($c))

evaluated with $collation as the default collation if specified, and with $c as the converted sequence.

Error Conditions

A type error is raised [ERRFORG0006] if the input sequence contains items of incompatible types, as described above.

Notes

Because the rules allow the sequence to be reordered, if there are two or more items that are "equal highest", the specific item whose value is returned is implementation-dependent. This can arise for example if two different strings compare equal under the selected collation, or if two different xs:dateTime values compare equal despite being in different timezones.

If the converted sequence contains exactly one value then that value is returned.

The default type when the fn:max function is applied to xs:untypedAtomic values is xs:double. This differs from the default type for operators such as gt, and for sorting in XQuery and XSLT, which is xs:string.

The rules for the dynamic type of the result are stricter in version 3.1 of the specification than in earlier versions. For example, if all the values in the input sequence belong to types derived from xs:integer, version 3.0 required only that the result be an instance of the least common supertype of the types present in the input sequence; Version 3.1 requires that the returned value retains its original type. This does not apply, however, where type promotion is needed to convert all the values to a common primitive type.

Examples

The expression fn:max((3,4,5)) returns 5.

The expression fn:max([3,4,5]) returns 5. (Arrays are atomized).

The expression fn:max((xs:integer(5), xs:float(5.0), xs:double(0))) returns xs:double(5.0e0).

fn:max((3,4,"Zero")) raises a type error [ERRFORG0006].

The expression fn:max((fn:current-date(), xs:date("2100-01-01"))) returns xs:date("2100-01-01"). (Assuming that the current date is during the 21st century.)

The expression fn:max(("a", "b", "c")) returns "c". (Assuming a typical default collation.)