Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding maximum value from nodeset with Xpath?

Status
Not open for further replies.

golcarlad

Programmer
Nov 23, 2004
232
GB
How do I return the maximum value for each ecoRule below using and Xpath expression? So for the first nodeset, I would get 10, and the second 9.
I used xpath code like this as a pure guess but it doesnt work - any ideas...(by the way Im programming in C#)

Code:
_doc.SelectNodes("//ecoRule[@name='"+ ecoRuleName +"']/max(scoreRule[@points])");


XML......below

Code:
<ecoRules>
 <ecoRule name="Something Else">
     <scoreRule type="ADJACENT" points="5" /> 
     <scoreRule type="CONTAINED" points="10" /> 
 </ecoRule>
 <ecoRule name="Adjacent">
     <scoreRule type="ADJACENT" points="3" /> 
     <scoreRule type="ADJACENT" points="9" /> 
     <scoreRule type="ADJACENT" points="7" /> 
   </ecoRule>
</ecoRules>
 
Hi,

Tried that - but I get the "Invalid Token" error....
 
Yes of course, because max is not a valid XPath function. [blush]

Trying again...

Code:
//ecoRule[@name='"+ ecoRuleName +"']/scoreRule[not(preceding-sibling::scoreRule/@points > @points or following-sibling::scoreRule/@points > @points)]

This should select the scoreRule node that does not have any sibling that has a points attribute value greater than the points attribute value of the selected node.

Clear? [ponder]

Tom Morrison
 
Of course, if you want the value of the points attribute, slightly modify the above as follows:
Code:
//ecoRule[@name='"+ ecoRuleName +"']/scoreRule[not(preceding-sibling::scoreRule/@points > @points or following-sibling::scoreRule/@points > @points)]/@points

Tom Morrison
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top