Mike,

Your response is extremely useful -- thank the gods for multiple OrderSpec expressions!

Regards,
Shannon

On May 21, 2008, at 3:22 PM, Michael Blakeley wrote:

This is one of the uglier warts in XQuery. http://www.w3.org/TR/xquery/#id-flwor-expressions and http://www.w3.org/TR/xquery/#prod-xquery-OrderModifier specify that OrderModifier is a literal token, not a variable or an expression. So it's tricky to write dynamic sorts.

But here is one approach, taking advantage of multiple OrderSpec expressions:

let $ascending := false()
for $i in 1 to 10
order by
 if ($ascending) then $i else () ascending,
 if ($ascending) then () else $i descending
return $i
=>
10 9 8 7 6 5 4 3 2 1

Another technique is to build a query with the desired sorting, then xdmp:eval() it. That's a very flexible approach, but somewhat error- prone. If you go down that road, be sure to use external variables to guard against escaping problems and query injections.

-- Mike

Shannon Scott Shiflett wrote:
hello,
is it possible in xquery flwor to use a conditional expression as the order modifier of the order by clause? i would like to specify "ascending" or "descending" dynamically.
thanks,
shannon
_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

_______________________________________________
General mailing list
[email protected]
http://xqzone.com/mailman/listinfo/general

Reply via email to