I was wondering on how to retrieve a xml node which conatains maximum value among a set of nodes. Initially I was thinking to write a Java programme to pass the XML and write logic to retrieve a node containing maximum value. But I found a quicker way of doing it using XPath.
Sample data and example XPath are:
<data> <message> <value>1</value> </message> <message> <value>3</value> </message> <message> <value>5</value> </message> <message> <value>2</value> </message> <message> <value>4</value> </message> </data>
In order to retrieve message node with value “5” which is maximum, I can use the following XPath (Remember that this is a string comparison)
//data/message[not(value <= preceding-sibling::message/value) and not(value <= following-sibling::message/value)]
In order to retrieve message node with value “1” which is minimum, I can use the following XPath (Remember that this is a string comparison)
//data/message[not(value >= preceding-sibling::message/value) and not(value >= following-sibling::message/value)]