Reputation: 1148
how i can increase loop fourteenth by fourteenth with xquery . For example i have this loop :
for $i in (0 to 150)
return $i
I want increase this loop fourteenth by fourteenth .Output:
14
28
42
56
70
etc.
I am working on exist-db.
Upvotes: 0
Views: 172
Reputation: 38712
Most simple – and probably easiest to read – approach would be to filter the sequence using the modulo function, but this has the disadvantage that all 150 values have to be constructed:
(1 to 150)[. mod 14 = 0]
Using the approach @prker used, you have to calculate the upper bound on your own. idiv
is the integer division operator.
for $i in (1 to 150 idiv 14)
return $i * 14
Using XQuery 3.0, you could also use the apply operator !
instead:
(1 to 150 idiv 14) ! (. * 14)
Or finally by using a recursive function:
declare function local:n-sequence($from as xs:integer, $to as xs:integer, $step as xs:integer) {
if ($from > $to)
then ()
else ($from, local:n-sequence($from + $step, $to, $step))
};
local:n-sequence(1, 150, 14)
Upvotes: 2