XPath Predicate

Predicate specifies XPath expressions written in square bracket. It is used to restrict the selected nodes in a node set for some conditions. See the use of predicate in the "employee" example.

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>
Index Predicate Description
1) /class/employee[1] It will select first employee element which is child of the class element.
2) /class/employee[last()] It will select last employee element which is child of the class element.
3) /class/employee[@id = 002] It will select employee element where id is 002.
4) /class/salary[salary > 10000] It will select employee element where salary is greater than 10000.

XPath Predicate Example

Let's take an example to create a table of <employee> element with their details, by iterating over each employee. This example uses predicate to calculate the position of the employee node and then print the employee details.

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>	  
               </tr>					
               <xsl:for-each select = "/class/employee[1]">
                  <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>	 
                  </tr>	
               </xsl:for-each>					
               <xsl:for-each select = "/class/employee[last()]">
                  <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>	 
                  </tr>	
               </xsl:for-each>	
               <xsl:for-each select = "/class/employee[@id = 002]">
                  <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>	 
                  </tr>	
               </xsl:for-each>
               <xsl:for-each select = "/class/employee[salary > 10000]">
                  <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>	 
                  </tr>	
               </xsl:for-each>		
            </table> 
         </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Output:

XPATH Predicate 1

Note:In the above example, you can see that all four queries are fetched.

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