"an axis ... specifies the tree relationship between the nodes selected by the location step and the context node."
This starts one wondering what a "tree relationship" is. By looking at the list of axes, we can first see some obvious relationships. For example, child and parent relationships are natural for trees. And you can extend this to the child of a child, or descendant, and to the parent of a parent, or ancestor. And self has got to be the context node itself. That allows the inclusion of ancestor-or-self and descendant-or-self in the set of what makes sense.
Axes
Here is a list of all the kinds of axes in XPath:
- ancestor
- ancestor-or-self
- attribute
- child
- descendant
- descendant-or-self
- following
- following-sibling
- namespace
- parent
- preceding
- preceding-sibling
- self
1. Company/Director/Manager
O/P:
Manager 1
Manager 2
Manager 3
2. descendant::Manager (or)
//Manager
O/P:
Manager 1
Manager 2
Manager 3
3. //Lead[@title="Lead 2"]/Reportee
O/P:
Reportee 3
Reportee 4
4. //Reportee[@title="Reportee 6"]/ancestor::*
O/P:
Company A
Director 3
Lead 3
5. //Reportee[@title="Reportee 6"]/ancestor-or-self::*
O/P:
Company A
Director 3
Lead 3
Reportee 6
6. //Lead[@title="Lead 1"]/ancestor::*
O/P:
Company A
Director 2
7. //Lead[@title="Lead 1"]/descendant::*
O/P:
Reportee 1
Reportee 2
8. //Lead[@title="Lead 1"]/following::*
O/P:
Director 3
Lead 2
Reportee 3
Reportee 4
Lead 3
Reportee 5
Reportee 6
9. //Lead[@title="Lead 1"]/preceding::*
O/P:
Director 1
Manager 1
Manager 2
Manager 3
10. //Lead[@title="Lead 1"]/self::*
O/P:
Lead 1
11. //Director[@title="Director 3"]/preceding-sibling::*
O/P:
Director 1
Director 2
12. //Reportee[@title="Reportee 6"]/ancestor::*
O/P:
Company A
Director 3
Lead 3
Now we can
add predicates that selects the first, second, and third members of this set to
see how the proximity position works on a reverse set:
//Reportee[@title="Reportee 6"]/ancestor::*[1]
O/P:
Company A
//Reportee[@title="Reportee 6"]/ancestor::*[2]
O/P:
Director 3
//Reportee[@title="Reportee 6"]/ancestor::*[3]
O/P:
Lead 3
13. //Director
O/P:
Director 1
Director 2
Director 3
//Director[1]
O/P:
Director 1
//Director[2]
O/P:
Director 2
//Director[3]
O/P:
Director 3
Nice Blog. Thanks for putting all together.
ReplyDeleteNice example
ReplyDelete