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
<?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. |
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
<?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
<?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:
Note:In the above example, you can see that all four queries are fetched.