[MarkLogic Dev General] order modifier

Michael Blakeley michael.blakeley at marklogic.com
Wed May 21 12:22:44 PDT 2008


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
> General at developer.marklogic.com
> http://xqzone.com/mailman/listinfo/general



More information about the General mailing list