将结果限制为 xquery 中的前四名
Limiting the result to top four in xquery
<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 页面上的子序列方法以及 [position() 1,4] 方法来查找前 4 个,但没有成功。
一种可能的方法是使用两个 for
循环并将内部 for
限制为 return 只有前 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>
更新:
事实证明我们实际上并不需要外部 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>
<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 页面上的子序列方法以及 [position() 1,4] 方法来查找前 4 个,但没有成功。
一种可能的方法是使用两个 for
循环并将内部 for
限制为 return 只有前 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>
更新:
事实证明我们实际上并不需要外部 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>