I have a rather long XML document with subsections under elements called <Pages>. Each of these pages contains an element called <NumberOfEntries> with an integer value.
I am trying to show the part of the XML from the <Page> which has the highest number of entries.
I have looked at the fn:max function, but it’s not working for me …
This is what I have tried –
for $x in /Document
where $x fn:max(//NumberOfEntries)
return $x//Page::node()
EDIT
I just worked out this one but it’s returning all the pages – not just the one with the highest number of entries – any suggestions?
for $x in /Document
where max($x//NumberOfEntries)
return ($x//Page)
As
max(...)doesn’t return boolean results,where max($x//NumberOfEntries)is equivalent towhere exists(max($x//NumberOfEntries)), so it isn’t filtering anything. You want to find the page that has the maximum number of entries, so here’s what I’d do:If more than one page has the same number of entries and that’s the maximum, they’re all returned. If you just want one result, you can wrap the last line into
(...)[1]to take the first one: