返回xquery中的所有最大值

时间:2013-12-04 16:17:56

标签: xml max xquery

我创建了以下xml文件

<?xml version="1.0" standalone="no"?>
<!DOCTYPE Movies SYSTEM "actressinfo.dtd">
<ActressInfo>
<Actress firstname="Robin" lastname="Nico" count="7"/>
<Actress firstname="some" lastname="chick" count="7"/>
<Actress firstname="random" lastname="chick" count="6"/>

</ActressInfo>

我想要检索所有等于xquery

中最大计数的值
for $p in doc("actressinfo.xml") //ActressInfo
where $p/Actress/@count = max($p/Actress/@count)
return  $p/Actress/@count

然而,这会归还一切,为什么会这样呢?尽管max返回的值是正确的。

1 个答案:

答案 0 :(得分:2)

您正在对ActressInfo进行迭代,因此$p/Actress/@count将返回每Actress个所有ActressInfo个计数。如果您希望每个Actress的最高计数ActressInfo

for $p in doc("actressinfo.xml")//ActressInfo/Actress
where $p/@count = max($p/ancestor::ActressInfo/Actress/@count)
return $p

或者对于匹配总体最大值的所有Actress元素:

let $max := max(doc("actressinfo.xml")//ActressInfo/Actress/@count)
for $p in doc("actressinfo.xml")//ActressInfo/Actress
where $p/@count = $max
return $p

如果您只有一个ActressInfo元素,那么这更有意义。如果有多个,并且您希望每Actress一个ActressInfo,则第一个查询可能更合适。