A.A
A.A

Reputation: 1148

How increase loop with Xquery?

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

Answers (2)

prker
prker

Reputation: 504

Try this:

for $i in (0 to 150)
return ($i+1)*14

Upvotes: 3

Jens Erat
Jens Erat

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

Related Questions