XPath/XQuery index-of function

Summary

Returns a sequence of positive integers giving the positions within the sequence $seq of items that are equal to $search.

Signatures

fn:index-of(
$seq as xs:anyAtomicType*,
$search as xs:anyAtomicType
) as xs:integer*
fn:index-of(
$seq as xs:anyAtomicType*,
$search as xs:anyAtomicType,
$collation as xs:string
) as xs:integer*

Properties

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

Rules

The function returns a sequence of positive integers giving the positions within the sequence $seq of items that are equal to $search.

The collation used by this function is determined according to the rules in . This collation is used when string comparison is required.

The items in the sequence $seq are compared with $search under the rules for the eq operator. Values of type xs:untypedAtomic are compared as if they were of type xs:string. Values that cannot be compared, because the eq operator is not defined for their types, are considered to be distinct. If an item compares equal, then the position of that item in the sequence $seq is included in the result.

The first item in a sequence is at position 1, not position 0.

The result sequence is in ascending numeric order.

Examples

The expression fn:index-of((10, 20, 30, 40), 35) returns ().

The expression fn:index-of((10, 20, 30, 30, 20, 10), 20) returns (2, 5).

The expression fn:index-of(("a", "sport", "and", "a", "pastime"), "a") returns (1, 4).

The expression fn:index-of(current-date(), 23) returns ().

The expression fn:index-of([1, [5, 6], [6, 7]], 6) returns (3, 4).

If @a is an attribute of type xs:NMTOKENS whose string value is "red green blue", and whose typed value is therefore ("red", "green", "blue"), then fn:index-of(@a, "blue") returns 3. This is because the function calling mechanism atomizes the attribute node to produce a sequence of three xs:NMTOKEN values.

Notes

If the value of $seq is the empty sequence, or if no item in $seq matches $search, then the function returns the empty sequence.

No error occurs if non-comparable values are encountered. So when comparing two atomic values, the effective boolean value of fn:index-of($a, $b) is true if $a and $b are equal, false if they are not equal or not comparable.