Finding Minimums and Maximums
Problem
You need to find the minimum (or maximum) numerical node (or nodes) in a node set.
Solution
The EXSLT functions that perform these operations are
math:min
, math:max
,
math:lowest
, and math:highest
.
min
and
max
find the value of the node with minimum and
maximum numerical value, respectively. EXSLT defines
math:min
as follows:
The minimum value is defined as follows. The node set passed as an argument is sorted in ascending order as it would be by xsl:sort with a data type of number. The minimum is the result of converting the string value of the first node in this sorted list to a number using the number function.
If the node set is empty, or if the result of converting the string values of any of the nodes to a number is NaN, then NaN is returned.
math:max
is defined similarly. EXSLT provides pure
XSLT implementations that are literal implementations of this
definition, as shown in Example 2-9.
Example 2-9. EXSLT min and max implement directly from the definition
<xsl:template name="math:min"> <xsl:param name="nodes" select="/.." /> <xsl:choose> <xsl:when test="not($nodes)">NaN</xsl:when> <xsl:otherwise> <xsl:for-each select="$nodes"> <xsl:sort data-type="number" /> <xsl:if test="position( ) = 1"> <xsl:value-of select="number(.)" /> </xsl:if> </xsl:for-each> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="math:max"> <xsl:param name="nodes" select="/.." /> <xsl:choose> <xsl:when test="not($nodes)">NaN</xsl:when> <xsl:otherwise> ...
Get XSLT Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.