145
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
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
Employee.xsl
Output:
Note:In the above example, you can see that all four queries are fetched.
Next Topic#