7

I have the following Xquery code:

for $w in $words
let $freq := (count($corpus[. eq $w]) div count($content2[text() eq $w])) 
order by $freq descending
return <tr><td>{$w}</td><td>{$freq}</td></tr>

$words is some distinct words that appears (may appear multiple times) in $corpus. $content2 is some another bag of words. Variables and division is not that important.

This xquery lists some frequency calculation of words, ordered.

What I want to do is limit the results by 10. I tried to use positional values but as it gives me the position of the word in the word list, it didnt work out..

Any help?

2 Answers 2

17

The number of results can e.g. be limited as follows:

(for $w in $words
 let $freq := (count($corpus[. eq $w]) div count($content2[text() eq $w])) 
 order by $freq descending
 return <tr><td>{$w}</td><td>{$freq}</td></tr>
)[position() = 1 to 10]
1
  • 2
    This only works with MarkLogic (it’s not part of the official specification). Jan 2, 2018 at 9:24
6

Use subsequence($sequence, $start, $records) to return a subset of results, starting at record $start (be aware XQuery counts from 1) returning records items. Have a look at "Limiting Result Sets" XQuery Wikibooks Page. An example of how to apply the function copied from this page:

let $sorted-people :=
   for $person in collection($collection)/person
   order by $person/last-name/text()
   return $person

for $person at $count in subsequence($sorted-people, $start, $records)
return
   <li>{ $person/last-name/text() }</li>
2
  • There are no curly brackets, the right version is <li>{$person/last-name/text()}</li>. Mar 11, 2016 at 12:23
  • 1
    Thank you for pointing this out, at the same time I improved the whole answer, anyway.
    – Jens Erat
    Mar 11, 2016 at 12:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.