Friday, 18 June 2010

MarkLogic: XQuery: hacking the position() - a basic example

I'd been wondering whether there was a way in a FLWOR statement to get the index position. I'd tried a few things that I'd hoped might work but I couldn't find a decent way to get back to the context whenever I got to the return portion of the statement.

It became apparent that in some (not all) contexts, I could rely on this example as a quick (and cheap) workaround:

Yields this:

Update - John Snelson advised me on the correct way to deal with position in XQuery - using for and at when starting the FLWOR statement:

Also worth considering:

And another useful positional trick, suggested by Jason Booth:


John Snelson said...

Try this:

for $record at $position in $recordset/record
return element item {attribute index {$position}, $record//name/text()}

Alex Bleasdale said...

That's excelllent, John - thanks so much for posting!