[xquery-talk] Finding context position when "order by" is in a FLWOR?

Jonathan Robie jonathan.robie at datadirect.com
Fri Sep 8 18:17:55 PDT 2006

Yeah, that's a pain - but that's the way it is. You want the numbering 
based on the generated tuples, the at clause gives the numbering based 
on each input individually, so the nested query you show is what I 
usually do.

One extension that someone suggested for XQuery is to allow an at in the 
return clause:

    let $list := ( 'cat', 'dog', 'apple')
    for $word in $list
    order by $word
    return at $pos <word n="{$pos}">{$word}</word>

*WAY* too late for XQuery 1.0, but I think this is very useful for 
future versions.


David Sewell wrote:
> Suppose I want to number output elements in a simply query containing an
> "order by". Alas,
> 	let $list := ( 'cat', 'dog', 'apple')
> 	for $word at $pos in $list
> 	order by $word
> 	return <word n="{$pos}">{$word}</word>
> produces
> 	<word n="3">apple</word>
> 	<word n="1">cat</word>
> 	<word n="2">dog</word>
> because the positional variable is bound in the "for" statement.
> I can't figure out a direct way to get @n to contain the value of output
> order; position() doesn't seem to help. The best approach I come up with
> is to wrap the above FLWOR in another one:
> 	for $item at $lineNo in
> 	   let $list := ( 'cat', 'dog', 'apple')
> 	   for $word at $pos in $list
> 	   order by $word
> 	   return $word
> 	return <word n="{$lineNo}">{$item}</word>
> which produces the desired output. Is there a better way?

Read my Blog: http://blogs.datadirect.com/jonathan_robie/
Learn XQuery: http://media.datadirect.com/download/docs/ddxquery/tutorial_query.html
Learn XQJ (the JDBC for XQuery): http://www.datadirect.com/developer/xquery/topics/xqj_tutorial/
Get DataDirect XQuery: http://www.datadirect.com/products/xquery/

More information about the talk mailing list