0
votes

I Created the following xml file

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

And I want to retrieve all values that are equal to max count in xquery

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

However this returns everything, why is it doing so? Despite the value returned by max being correct.

1

1 Answers

2
votes

You're iterating over ActressInfo so $p/Actress/@count will return all Actress counts from every ActressInfo. If you want the highest count Actress for each ActressInfo:

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

Or for all Actress elements matching the overall maximum:

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

If you only have one ActressInfo element, then this makes more sense. If there are multiple, and you want one Actress per ActressInfo, then the first query is probably a better fit.