chintogtokh
chintogtokh

Reputation: 803

XPath getting a node back using a max value

Say I have a bunch of elements like this:

<rs>
 <r>
  <roomID>ID2</roomID>
 </r>
 <r>
  <roomID>ID2</roomID>
 </r>
 <r>
  <roomID>ID3</roomID>
 </r>
 <r>
  <roomID>ID2</roomID>
 </r>
</rs>

<rooms>
 <room id="ID1">
  <val>200</val>
 </room>
 <room id="ID2">
  <val>100</val>
 </room>
 <room id="ID3">
  <val>200</val>
 </room>
</rooms>

I need to cycle through rs, and find the r that has the highest room value based on the roomID, which would be the third r element here (with roomID=ID3).

If I do this:

<xsl:variable name="max1" select="max(//room[@id=//r/roomID]/val)"/>

, I get 200, which is correct. But is there any way to get the specific ID back from this? If I just use the max value to get back the ID from the room elements, I can't know if it comes back from the same node. Something like this:

//room[val=max(//room[@id=//r/roomID]/val)]/@id[1]

Upvotes: 0

Views: 51

Answers (2)

hr_117
hr_117

Reputation: 9627

Your xapth has only a small mistake Put the expression in parentheses (...) before selecting the first one [1]. Try:

 (//room[val = max(//room[@id=//r/roomID]/val)]/@id)[1]

Upvotes: 0

chintogtokh
chintogtokh

Reputation: 803

I suppose this works:

<xsl:value-of select="//r[roomID=//room[val=max(//room[@id=//r/roomID]/val)]/@id]"/>

Seems a bit messy, though.

Upvotes: 1

Related Questions