Indexes a sequence as a map.
index($sequence as item()*, $function as function(item()) as xs:anyAtomicType*) ➔ map(*)
The sequence to be indexed
function(item()) as xs:anyAtomicType*
Computes the value(s) on which the sequence is indexed
Requires Saxon-PE or Saxon-EE.
Notes on the Saxon implementation
Available since Saxon 8.3. Changed in Saxon 9.5 to take a function as the second argument, and to return a map as the result; and the collation argument was dropped.
The first argument is any sequence. Usually it will be a sequence of nodes, but this is not essential. This is the sequence being indexed.
The second argument is a function used to compute a key. This function is called once for each item in the sequence being indexed, with that item as the argument. The result of the function is atomized. Each value in the atomized result represents a key value: the item in the indexed sequence can be efficiently found using any of these key values.
If a key value is of type
xs:untypedAtomic, it is treated as a string. If
you want to treat the value as numeric, say, then perform a conversion within the
The result is a map, that can be supplied as input to any map functions such as
For example, consider a source document of the form:<doc> <town name="Amherst" state="NH"/> <town name="Amherst" state="MA"/> <town name="Auburn" state="MA"/> <town name="Auburn" state="NH"/> <town name="Auburn" state="ME"/> <town name="Bristol" state="RI"/> <town name="Bristol" state="ME"/> <town name="Bristol" state="CT"/> <town name="Bristol" state="NH"/> <town name="Bristol" state="VT"/> <town name="Cambridge" state="ME"/> </doc>
and suppose there is a requirement to find
efficiently given the abbreviation for the
state. You can do this by first
setting up an indexed sequence. In XQuery you can write:
This could be a local variable (declared in a
let clause) rather than a
global variable. The XSLT equivalent is:
You can then find all the towns in New Hampshire using the expression:map:get($indexedTowns, "NH")
Indexed sequences were introduced primarily for use in XQuery, where they provide
functionality equivalent to the standard
xsl:key mechanism in XSLT. From
Saxon 9.5 the data structure used for an indexed sequence is a map, and with the
introduction of maps the function could now be implemented simply as:
For further information about maps see Maps in XPath.