saxon:slice

Selects items from a sequence based on their position.

slice($seq as item()*, $positions as xs:integer*) ➔ item()*

Arguments

 

$seq

item()*

The sequence to be filtered

 

$positions

xs:integer*

The positions of the items to be selected

Result

item()*

Namespace

http://saxon.sf.net/

Saxon availability

Requires Saxon-PE or Saxon-EE. Implemented since Saxon 11. Available for all platforms.

Notes on the Saxon implementation

Available since Saxon 11. The function is likely to become superseded by the proposed XPath 4.0 function fn:slice(), which has a slightly different specification. All the functionality except for the use of negative positions (to count from the end) is already available using the XPath 4.0 function fn:items-at(), and other common use cases can be achieved using, for example, fn:foot() and fn:trunk().

Details

For each integer $i in the value of $positions, in order, the function returns:

  • $seq[$i] if the integer is between 1 and the length of $seq (inclusive)
  • $seq[count($seq)-$i+1] if the integer is negative and its absolute value is between 1 and the length of $seq (inclusive)
  • an empty sequence if the integer is zero, or if its absolute value exceeds the length of $seq

For example:

  • saxon:slice(10 to 20, 5) returns 14
  • saxon:slice(10 to 20, 5 to 6) returns (14, 15)
  • saxon:slice(10 to 20, (5, 3, 1)) returns (14, 12, 10)
  • saxon:slice(10 to 20, -1) returns 20
  • saxon:slice(10 to 20, -3 to -1) returns (18, 19, 20)
  • saxon:slice(10 to 20, 0) returns ()
  • saxon:slice(10 to 20, -2 to +2) returns (19, 20, 10, 11)