我创建了以下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返回的值是正确的。
答案 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
,则第一个查询可能更合适。