The XPath Numeric Operators and Functions

XPath 1.0 has the following operators you can use on numbers :

  • + performs addition.

  • - performs subtraction.

  • * performs multiplication.

  • div performs division (the / character, which stands for division in other languages, is already heavily used in XML and XPath).

  • mod returns the modulus of two numbers (the remainder after dividing the first by the second).

For example, the location path //planet[1 + 4] will find the fifth <planet> element, //planet[2 * 3] will find the sixth , and so on. Here's an XSLT examplethis example selects all planets whose radius (measured in miles) divided by its day (measured in earth days) is greater than 120:

 
 <xsl:template match="planets">     <HTML>         <BODY>             <xsl:apply-templates select="planet[radius div day > 120]"/>         </BODY>     </HTML> </xsl:template> 

In addition, XPath 1.0 supports these 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.

We'll take a look at each of these functions to become familiar with them.

The ceiling Function

You use the ceiling function to get the smallest integer that is larger than the number you pass it. For example, this expression returns 4:

 
 ceiling(3.1415926535) 

The floor Function

The floor function returns the largest integer that is smaller than the number you pass it. For example, this expression returns 3:

 
 floor(3.1415926535) 

You can see an XSLT example in ch04_02.xsl in Listing 4.1, where we're converting the data in the planetary data document, ch04_01.xml , using floor to convert the value of the <distance> element to an integer.

Listing 4.1 Using the Floor Function ( ch04_02.xsl )
 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="/planets">         <HTML>             <HEAD>                 <TITLE>                     Planetary Data                 </TITLE>             </HEAD>             <BODY>                 <H1>                     Planetary Data                 </H1>                 <TABLE BORDER="2">                     <TR>                         <TD>Name</TD>                         <TD>Radius</TD>                         <TD>Distance</TD>                     </TR>                     <xsl:apply-templates/>                 </TABLE>             </BODY>         </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="distance"/></TD>        </TR>    </xsl:template>     <xsl:template match="radius">         <xsl:value-of select="."/>         <xsl:text> </xsl:text>         <xsl:value-of select="@units"/>     </xsl:template>     <xsl:template match="distance">  <xsl:value-of select="floor(.)"/>  <xsl:text> </xsl:text>         <xsl:value-of select="@units"/>     </xsl:template> </xsl:stylesheet> 

And here's the result when ch04_02.xsl is applied to ch04_01.xml :

 
 <HTML>    <HEAD>       <TITLE>          Planetary Data       </TITLE>    </HEAD>    <BODY>       <H1>          Planetary Data       </H1>       <TABLE BORDER="2">          <TR>             <TD>Name</TD>             <TD>Radius</TD>             <TD>Distance</TD>          </TR>          <TR>             <TD>Mercury</TD>             <TD>.0553</TD>             <TD>1516 miles</TD>  <TD>43 million miles</TD>  </TR>          <TR>             <TD>Venus</TD>             <TD>.815</TD>             <TD>3716 miles</TD>  <TD>66 million miles</TD>  </TR>          <TR>             <TD>Earth</TD>             <TD>1</TD>             <TD>2107 miles</TD>  <TD>128 million miles</TD>  </TR>       </TABLE>    </BODY> </HTML> 

The number Function

The number function just converts its argument to a number. Here's an example, where we're passing the string "3.1415" to this function:

 
 number("3.1415") 

This expression returns the number 3.1415.

The round Function

The round function rounds its value and returns it. For example, round(3.1415926535) returns 3, round(5.5) returns 6, round(-2.5) returns -2, and so on.

The sum Function

The sum function adds together the numeric values of a set of nodes and returns the result. For example, we can find the average planetary mass using the expression sum(//mass) div count(//mass) , where the count function is a node-set function we'll see later in this chapter that returns the number of nodes in a node-set. You can see the results displayed by the XPath Visualiser in Figure 4.3.

Figure 4.3. Using the sum function.

graphics/04fig03.jpg

Here's how you can create the same result in XSLT:

 
 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.1" 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(//mass) div count(//mass)"/>  </BODY>     </HTML> </xsl:template> </xsl:stylesheet> 

And here's the result document:

 
 <?xml version="1.0" encoding="UTF-8"?> <HTML>     <BODY>         The average planetary mass is: 0.6234333333333334     </BODY> </HTML> 

That finishes our look at the XPath 1.0 numeric operators and functions. Next, we'll take a look at the XPath 1.0 string operators and functions.



XPath. Navigating XML with XPath 1.0 and 2.0 Kick Start
XPath Kick Start: Navigating XML with XPath 1.0 and 2.0
ISBN: 0672324113
EAN: 2147483647
Year: 2002
Pages: 131

Similar book on Amazon

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net