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

David Sewell dsewell at virginia.edu
Fri Sep 8 17:25:31 PDT 2006

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>


	<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?

David Sewell, Editorial and Technical Manager
ROTUNDA, The University of Virginia Press
PO Box 400318, Charlottesville, VA 22904-4318 USA
Courier: 310 Old Ivy Way, Suite 302, Charlottesville VA 22903
Email: dsewell at virginia.edu   Tel: +1 434 924 9973
Web: http://rotunda.upress.virginia.edu/

More information about the talk mailing list