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/

Notes on the Saxon implementation

Available since Saxon 11.

Details

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

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

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, 1 by 3 to 7) returns (10, 13, 16)
  • saxon:slice(10 to 20, -1 by -1 to -4) returns (20, 19, 18, 17)
  • saxon:slice(10 to 20, 0) returns ()
  • saxon:slice(10 to 20, -2 to +2) returns (19, 20, 10, 11)