XPath Node Functions

A list of operators on nodes to be used with the XPath expression:

Index Operator Description
1) / It is used to select node under a specific node.
2) // It is used to select node from root node.
3) [...] It is used to check node value.
4) | It is used for union of two node sets.

A list of functions on nodes to be used with the XPath expression:

Index Function Description
1) node() It is used to select all kinds of nodes.
2) processing-instruction() It is used to select nodes which are processing instruction.
3) text() It is used to select a text node.
4) name() It is used to provide the name of the node.
5) position() It is used to provide the position of the node.
6) last() It is used to select the last node relative to current node;
7) comment() It is used to select nodes which are comments.

XPath Node Function Example

Let's take an example to create a table of <employee> element with their details, by iterating over each employee. It calculates the position of the student node then displays details of the employee with serial number.

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>Serial No</th>
                  <th>ID</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Nick Name</th>
                  <th>Salary</th>	  
               </tr>					 
               <xsl:for-each select = "class/employee">
                  <tr>
                     <td><xsl:value-of select = "position()"/></td>
                     <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>	 
                  </tr>	
               </xsl:for-each>					
            </table>
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output:

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