XPath Number Operators/ Functions

A list of number operators that are used with XPath expressions:

Index Operator Description
1) + It is used for addition operation.
2) - It is used for subtraction operation.
3) * It is used for multiplication operation.
4) div It is used for division operation.
5) mod It is used for modulo operation

A list of functions on numbers that are used with XPath expressions:

Index Function Description
1) ceiling() It is used to return the smallest integer larger than the value provided.
2) floor() It is used to return the largest integer smaller than the value provided.
3) round() It is used to return the rounded value to nearest integer.
4) sum() It is used to return the sum of two numbers.

XPath Number Operators/ Functions Example

Let's take an example to create a table of <employee> element with its attribute id and its child <firstname>,<lastname> <nickname> and <salary>. It calculates salary of the employees and then displays the result.

Employee.xml

snippet
<?xml version = "1.0"?>
<?xml-stylesheet type = "text/xsl" href = "employee.xsl"?>
<class>
   <employee id = "001">
      <firstname>Abhiram</firstname>
      <lastname>Kushwaha</lastname>
      <nickname>Manoj</nickname>
      <salary>15000</salary>
   </employee>
   <employee id = "002">
      <firstname>Akash</firstname>
      <lastname>Singh</lastname>
      <nickname>Bunty</nickname>
      <salary>25000</salary>
   </employee>
    <employee id = "003">
      <firstname>Brijesh</firstname>
      <lastname>Kaushik</lastname>
      <nickname>Ballu</nickname>
      <salary>20000</salary>
   </employee>
    <employee id = "004">
      <firstname>Zoya</firstname>
      <lastname>Mansoori</lastname>
      <nickname>Sonam</nickname>
      <salary>30000</salary>
   </employee>
</class>

Employee.xsl

snippet
<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">  
   <xsl:template match = "/">
      <html>
         <body>
            <h2>Employee</h2>				
            <table border = "1">
               <tr bgcolor = "pink">
                  <th>ID</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Nick Name</th>
                  <th>Salary</th>
                  <th>Grade</th>
               </tr>					
               <xsl:for-each select = "class/employee">
                  <tr>
                     <td><xsl:value-of select = "@id"/></td>
                     <td><xsl:value-of select = "firstname"/></td>
                     <td><xsl:value-of select = "lastname"/></td>
                     <td><xsl:value-of select = "nickname"/></td>
                     <td><xsl:value-of select = "salary"/></td>				
                     <td>
                        <xsl:choose>
                           <xsl:when test = "salary div 25000 > 1">
                              High
                           </xsl:when>							
                           <xsl:when test = "salary div 20000 > 1">
                              Medium
                           </xsl:when>							
                           <xsl:otherwise>
                              Low
                           </xsl:otherwise>
                        </xsl:choose>
                     </td>
                  </tr>	
               </xsl:for-each>
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output:

XPATH Number operators functions 1
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents +