XPath/XQuery min function

Summary

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

Signatures

fn:min(
$arg as xs:anyAtomicType*
) as xs:anyAtomicType?
fn:min(
$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 implicit-timezone.
The one-argument form of this function is deterministic, context-dependent, and focus-independent. It depends on collations static-base-uri implicit-timezone.

Rules

The following rules are applied to the input sequence:

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

  • If the resulting sequence contains values that are instances of more than one primitive type (meaning the 19 primitive types defined in ), then:

    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.

    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.

    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.

    Otherwise, a type error is raised .

    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 empty sequence is returned.

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 .

The function returns the result of the expression:

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

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

Examples

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

The expression fn:min([3,4,5]) returns 3.

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

fn:min((3,4,"Zero")) raises a type error .

fn:min((xs:float(0.0E0), xs:float(-0.0E0))) can return either positive or negative zero. The two items are equal, so it is implementation-dependent which is returned.

The expression fn:min((fn:current-date(), xs:date("1900-01-01"))) returns xs:date("1900-01-01").

The expression fn:min(("a", "b", "c")) returns "a".

Error Conditions

A type error is raised 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 items that are "equal lowest", 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:min function is applied to xs:untypedAtomic values is xs:double. This differs from the default type for operators such as lt, 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.