Youssef CHARIF
Youssef CHARIF

Reputation: 7

How to retrieve max value with xsl:sort

I have a xml file which contains multiple elements of type 'advert'. This last contains as a sub-element price.

I wanna extract the max price value using xsl:sort and xsl:for-each which browse all the items.

Upvotes: 0

Views: 1615

Answers (1)

Shror
Shror

Reputation: 66

As stated in the comments for other members, max simply could be used as is without the need of xslt sort and xslt for-each.

To be able to use the xsl in xsl as is, here is a simple piece of code to try:

<xsl:variable name="the_max">
<xsl:for-each select="Node/Item/year">
<xsl:sort data-type="number" order="descending"/>
<xsl:if test="position()=1"><xsl:value-of select="."/></xsl:if>
</xsl:for-each>
</xsl:variable>
<xsl:value-of select="$the_max"/>

using XSL values as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Node>
<Item><year>1985</year></Item>
<Item><year>1986</year></Item>
<Item><year>1987</year></Item>
<Item><year>1988</year></Item>
<Item><year>1989</year></Item>
<Item><year>1909</year></Item>
<Item><year>1991</year></Item>
<Item><year>1992</year></Item>
<Item><year>1993</year></Item>
<Item><year>1994</year></Item>
<Item><year>1995</year></Item>
<Item><year>1996</year></Item>
</Node>

Upvotes: 2

Related Questions