Forum Moderators: coopster & phranque

Message Too Old, No Replies

Finding the maximum

using XSLT

         

joshie76

10:54 am on Oct 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not sure where to post this, as the process is happening on the server (for us) I'll put it here; feel free to move it if there is somewhere more appropriate...

Take the following simplified XML slice:


<fields>
<field>
<number>4</number>
</field>
<field>
<number>3</number>
</field>
<field>
<number>2</number>
</field>
<field>
<number>6</number>
</field>
</fields>

How do I find out in XSL what the highest number is? In the example it's 6 - how do I get this out? Some kind of max(/fields/field/number) function or a cheeky bit of sorting and using the [last()] predicate but I can't get it to work...

Help!

Thanks in advance...

joshie76

12:13 pm on Oct 9, 2001 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Cracked it... (I keep doing this lately, posting and then solving - I promise I am trying to solve it myself for some time before posting!!!!).

For anyone who's interested:


<xsl:for-each select="/fields/field/number">
<xsl:sort select="/fields/field/number" data-type="number" order="ascending"/>
<xsl:if test="position() = last()">
<xsl:value-of select="."/><!-- this is where you will see the maximum value -->
</xsl:if>
</xsl:for-each>