2
<a>
{
for $o in doc("x.xml")/users/org
where fn:count($o/mem)
order by fn:count($o/mem) descending
return <org>
            <b> {$o/@name/string()} </b>
            <c> {$o/@ab/string()} </c>
            <d> {fn:count($o/mem)} </d>
       </org>
}
</a>

我已按降序排列结果,但我只需要前 4 个结果。我尝试使用 wiki 页面上的 sub-sequence 方法以及 [position() 1,4] 方法来查找前 4 名,但没有成功。

4

1 回答 1

1

使用两个for循环并限制内部for仅返回前 4 个结果的一种可能方法:

<a>
{
    for $p in
    (
        for $o in doc("x.xml")/users/org
        where fn:count($o/mem)
        order by fn:count($o/mem) descending
        return <org>
                    <b> {$o/@name/string()} </b>
                    <c> {$o/@ab/string()} </c>
                    <d> {fn:count($o/mem)} </d>
               </org>
    )[position() <= 4]
    return $p
}
</a>

更新 :

事实证明,我们实际上并不需要 external for

<a>
{
    (
        for $o in doc("x.xml")/users/org
        where fn:count($o/mem)
        order by fn:count($o/mem) descending
        return <org>
                    <b> {$o/@name/string()} </b>
                    <c> {$o/@ab/string()} </c>
                    <d> {fn:count($o/mem)} </d>
               </org>
    )[position() <= 4]
}
</a>
于 2015-11-20T07:37:41.273 回答