Given this XML:
<foos>
<foo>
<name>Apple</name>
<rank>1</rank>
<key>asdfasdfasdf</key>
</foo>
<foo>
<name>Orange</name>
<rank>3</rank>
<key>qwerqwtwetqw</key>
</foo>
<foo>
<name>Apple</name>
<rank>2</rank>
<key>zxcvzxvcxzb</key>
</foo>
...
</foos>
I’m trying to find the key of the foo with a particular name, let’s say “Apple”, that has the highest rank. I’m using the following xpath expression (a workaround for the lack of a max function):
/foos/foo[name='Apple' and rank >= /foos/foo[name='Apple']/rank]/key/text()
However I’m sometimes getting multiple results back, even though every name-rank combination is unique. Where am I going wrong here?
This is being used in Java – if that turns out to be relevant I can post the entire code.
Try this: