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

2 つのforループを使用し、内側を制限しforて最初の 4 つの結果のみを返す 1 つの方法:

<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>

アップデート :

実際にはアウターは必要ないことがわかりました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 に答える