From: Wolfgang M. <wol...@ex...> - 2004-02-27 10:28:23
|
Hi, > I'm new to XQuery and I wonder if it is possible to limit the number of > result nodes in the XQuery instead of giving it as a parameter to the eXist > method? Like "LIMIT 10" for example in sql. 1) You can use the item-at() function to access an item in a sequence. Using this, I can output the first 10 items of a sequence as follows: let $hits := (for $c in /country order by $c/city return $c) for $p in 1 to 10 return item-at($hits, $p) 2) the subsequence() function makes this even simpler: let $hits := (for $c in /country order by $c/city return $c) return subsequence($hits, 1, 10) > Then I wonder if it is possible to request random order, like "ORDER BY > RAND()" in sql. preferably, this should be possible to do not only on top > level but also in a hierarcy like "order by $a/country ascending, order by > random" that would mean the list would definitely be ordered by country > names. However within next level (if there are duplicate country names but > different {$a/country/city}) the $a nodes wouldn't appear exactly as stored > in the database, neither alphabetized but randomly. By default, all XQuery sequences are returned in document order. To return sequence values in a random order, you could use order by with some randomized value, e.g. by calling java.lang.Math.random xquery version "1.0"; declare namespace math="java:java.lang.Math"; let $hits := //SPEECH[LINE &= 'curse*'] for $item in $hits order by math:random() return $item Wolfgang |