Saxonica.com

saxon:index()

saxon:index($sequence as item()*, $expression as jt:net.sf.saxon.functions.Evaluate-PreparedExpression) ==> jt:com.saxonica.extra.IndexedSequence

saxon:index($sequence as item()*, $expression as jt:net.sf.saxon.functions.Evaluate-PreparedExpression, $collation as xs:string) ==> jt:com.saxonica.extra.IndexedSequence

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 compiled XPath expression. Most commonly, the argument will be written as a call to the saxon:expression() extension function. This expression is evaluated once for each item in the sequence being indexed, with that item as the context node. (The context position and size reflect the position of this item in the sequence, but this will not normally be useful.) The result of the expression 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 expression.

The optional third argument is the URI of a collation to be used when comparing strings. For example, if you want string matching to be accent- and case-blind, specify "http://saxon.sf.net/collation?strength=primary".

The result is an object of type {http://saxon.sf.net/java-type}com.saxonica.extra.IndexedSequence, that can be supplied as input to the saxon:find() function.

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 town elements efficiently given the abbreviation for the state. You can do this by first setting up an indexed sequence. In XQuery you can write:

declare namespace saxon="http://saxon.sf.net/";
 declare namespace java="http://saxon.sf.net/java-type";
 declare variable $indexedTowns 
    as java:com.saxonica.extra.IndexedSequence
    := saxon:index(//town, saxon:expression("@state"));

This could be a local variable (declared in a let clause) rather than a global variable. The XSLT equivalent is:

<xsl:variable name="indexedTowns" 
           select="saxon:index(//town, saxon:expression('@state'))"
           as="java:com.saxonica.extra.IndexedSequence"/>

You can then find all the towns in New Hampshire using the expression:

saxon:find($indexedTowns, "NH")

Indexed sequences are primarily useful in XQuery, where they provide functionality equivalent to the standard xsl:key mechanism in XSLT. There are some cases, however, where indexed sequences can also be useful in XSLT. One example is where there is a need for an index to span multiple documents: the XSLT key() function will only search within a single document.

An indexed sequence can only be used in the first argument to the saxon:find() function. If you want access to the sequence that was passed as the first argument to saxon:index(), you can get this by calling saxon:find() with a single argument.

The saxon:index function is available only with Saxon-SA.

For examples of use, see saxon:index().

Next