XQuery函数限制输出中的记录数

时间:2015-11-23 00:57:38

标签: xml xslt xquery

我有一个XQuery文件,它在输出中返回大量数据。我只想在输出中显示前5条记录。这是xquery代码。

<html>
<body>
<table border='1'>
<tr><td>Target</td><td>Successor</td><td>Probability</td></tr>

{

let $target := "has"

let $occurrences := doc("KS0.xml")//u//s/w[lower-case(normalize-space()) = $target]

for $successor in distinct-values($occurrences/following-sibling::w[1])

let $frequency := $occurrences/following-sibling::w[1][. = $successor]

let $probability := count($frequency) div count(//u//s/w[lower-case(normalize-space()) = lower-case(normalize-space($successor))])

order by count($frequency) descending

return <tr>
           <td>{$target}</td>
           <td>{$successor}</td>
           <td>{$probability}</td>
       </tr>
}

</table>
</body>
</html>

1 个答案:

答案 0 :(得分:2)

您的查询将返回一系列项目,因此您只需使用谓词通过将查询包装在括号中来限制从序列返回的项目数:

(
let $target := "has"
... 
</tr>
)[position() le 5]