2

I'm interested in getting back a specific range of results, say from 99 to 199, or, perhaps, the last JSON object. While this works for small numbers or ranges, how can it be generalized or expanded to a larger range?

xquery version "3.0";

for $i in (1,2,3)
for $line in db:open("json_people")
return $line/json/_[$i]
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21
Thufir
  • 8,216
  • 28
  • 125
  • 273
  • 1
    It’s not clear to me what you want to achieve. Most of all, I am confused about the naming of your variables. Does `db:open` really return lines? Could you please replace the database function with an XML snippet that allows us to run your code without further modifications? – Christian Grün Feb 17 '20 at 16:24
  • it wasn't anything specifically, just to get a range of results. The `line` is a bad name, yes. I kept trying from (99..199) or other variations but couldn't find the correct syntax. – Thufir Feb 17 '20 at 16:42

2 Answers2

3

You can avoid the nested loop, use the position() function and specify a range:

db:open("json_people")/json/_[position() = 99 to 199]
Christian Grün
  • 6,012
  • 18
  • 34
  • 1
    Or if you prefer, `subsequence(db:open("json_people")/json/_, 99, 100)`. Note that once again, fixating on finding a FLWOR expression to do this doesn't actually help you. – Michael Kay Feb 17 '20 at 18:15
  • fair enough, @MichaelKay -- I was also taking the opportunity to learn flwor a bit and at the same time didn't want stray. – Thufir Feb 27 '20 at 11:36
1

You can use range in XQuery as follows:

for $i in (99 to 199)
Yitzhak Khabinsky
  • 18,471
  • 2
  • 15
  • 21