You can use the preceding
and following
XPath axes to check whether there is no greater value:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<documents>"
+ " <document><id>3</id></document>"
+ " <document><id>7</id></document>"
+ " <document><id>1</id></document>"
+ "</documents>");
var max = doc.SelectSingleNode(
"/documents/document[not(id < preceding::document/id)
and not(id < following::document/id)]");
If there are several maximum id values in the document the above code will return the first one. If you want the last element with a maximum id you can use
var max = doc.SelectSingleNode(
"/documents/document[not(id < preceding::document/id)
and not(id <= following::document/id)]");
If you want to get a list with all elements having a maximum id you can use the SelectNodes
method:
var maxList = doc.SelectNodes(
"/documents/document[not(id < preceding::document/id)
and not(id < following::document/id)]");
XSLT version:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<root>
<xsl:value-of
select="/documents/document/id[not(. <= preceding::document/id)
and not(. <= following::document/id)]"/>
</root>
</xsl:template>
</xsl:stylesheet>