1
votes

I am fairly new to XSLT so still learning the basics. In the following XML sample I am interested in selecting all the nodes with the highest number value for the attribute "Version":

 <Results>
     <Result Version="1">a</Result>
     <Result Version="2">a</Result>
     <Result Version="3">b</Result>
     <Result Version="3">c</Result>
     <Result Version="1">d</Result>
     <Result Version="3">e</Result>
     <Result Version="2">f</Result>
     <Result Version="3">g</Result>
     <Result Version="1">h</Result>
 </Results>

So in the above example I am interested in selecting the nodes with the values: b,c,e and g.

Hope my question and example makes sense!

Thanks for any help!

1
sorry should have clarified, 2.0 - user2411083

1 Answers

3
votes

I would start by defining a key:

<xsl:key name="result-by-version" match="Result" use="@Version" />

Then you can use:

select="key('result-by-version', xs:string(max(Result/@Version)))"

to select all results with the maximum version (in this example the context node is Results).


Alternatively, you could use a more pedestrian:

select="Result[@Version = (max(../Result/@Version))]"

(this too is from the context of Results).