XPath supports the following functions that operate on numbers :
ceiling() . Returns the smallest integer larger than the number you pass it.
floor() . Returns the largest integer smaller than the number you pass it.
round() . Rounds the number you pass it to the nearest integer.
sum() . Returns the sum of the numbers you pass it.
The following sections provide examples for each of these functions.
The ceiling function returns the smallest integer that is still larger than the number you pass it; that is, it returns the next greater integer. Heres how you use this function:
number ceiling(number)
For example, this expression
ceiling(3.1415926535)
returns 4.
The floor function is the counterpart to the ceiling function. This function returns the largest integer that is still smaller than the number you pass it. In other words, this function returns the preceding integer. Heres how you use this function:
number floor(number)
In this example, I use floor to convert the length of planetary days to integers:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/PLANETS"> <HTML> . . </HTML> </xsl:template> <xsl:template match="PLANET"> <TR> <TD><xsl:value-of select="NAME"/></TD> <TD><xsl:apply-templates select="MASS"/></TD> <TD><xsl:apply-templates select="RADIUS"/></TD> <TD><xsl:apply-templates select="DAY"/></TD> </TR> </xsl:template> <xsl:template match="MASS"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="@UNITS"/> </xsl:template> <xsl:template match="RADIUS"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="@UNITS"/> </xsl:template> <xsl:template match="DAY"> <xsl:value-of select="floor(.)"/> <xsl:text> </xsl:text> <xsl:value-of select="@UNITS"/> </xsl:template> </xsl:stylesheet>
And heres the result document:
<HTML> <HEAD> <TITLE> The Planets Table </TITLE> </HEAD> <BODY> <H1> The Planets Table </H1> <TABLE BORDER="2"> <TR> <TD>Name</TD> <TD>Mass</TD> <TD>Radius</TD> <TD>Day</TD> </TR> <TR> <TD>Mercury</TD> <TD>.0553 (Earth = 1)</TD> <TD>1516 miles</TD> <TD>58 days</TD> </TR> <TR> <TD>Venus</TD> <TD>.815 (Earth = 1)</TD> <TD>3716 miles</TD> <TD>116 days</TD> </TR> <TR> <TD>Earth</TD> <TD>1 (Earth = 1)</TD> <TD>2107 miles</TD> <TD>1 days</TD> </TR> </TABLE> </BODY> </HTML>
The number function converts its argument to a number. Heres how you use this function:
number number(object?)
In this example, Im passing number a string:
number("456.7")
This expression returns the number 456.7. If you try to convert something that number cant translate into a number, you get the XPath NaN (Not a Number) value. NaN is a legal value you can test for in XPath expressions.
The round function rounds its value. Heres how you use this function:
number round(number)
For example, round(3.1415926535) returns 3, round(4.5) returns 5, and round(-1.5) returns -1.
The sum function sums the numeric values of a set of nodes and returns the result. Heres how you use this function:
number sum(node-set)
Look at this example from Chapter 4, which found the average mass of the planets in planets.xml:
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml"/> <xsl:template match="PLANETS"> <HTML> <BODY> The average planetary mass is: <xsl:value-of select="sum(child::PLANET/child::MASS) div count(child::PLANET/ child::MASS)"/> </BODY> </HTML> </xsl:template> </xsl:stylesheet>