Saturday, 23 January 2016

Xpath Axes


Context Node:

Context node is start node in xpath

Ex :

URL = https://login.yahoo.com/
XPath = //input[@id='login-passwd']/following::input


Here

input[@id='login-passwd']  is context node.



There are following XPath Axes are available


1. Child Axes

Its identify the child node of the context node.

Ex:

URL = http://www.w3schools.com/xml/default.asp
XPath = //div[@id='leftmenu']/child::*
Output = Its identify the child node(<div id="leftmenuinner") of the context node (div id="leftmenu").


2. Parent Axes

Its identify the parent node of context node.

Ex:

URL = http://www.w3schools.com/xml/default.asp
XPath = //div[@id='leftmenu']/parent::*
Output = Its identify the parent node(<div id="belowtopnav") of the context node (div id="leftmenu").


3. Following Axes

Its identify the all node which is immediately match after current context node.

Ex:

URL = https://login.yahoo.com/

Xpath = //input[@id='login-passwd']/following::input

Output = Returns 7 immediate matching node after current context node.So it wont see any node, it will return all immediate matching node.


4. Following Sibling Axes

Its return the all node which is immediately match after context node under same parent node (Return sibling node only)

Ex:

URL = http://www.w3schools.com/xml/default.asp

Xpath= //a[contains(.,'XML Namespaces')]/following-sibling::a

Output =  Returns 8 immediate matching node after current context node. So it will see the parent node (<div>) and returns the only matching sibling nodes.


5. Preceding Axes

Its Identify the all node which is immediately match with before current context node.

Preceding Axes is opposite of Following Axes.


Ex:

URL = https://login.yahoo.com/

.//*[@id='login-signin']/preceding::input

Output = Returns 3 immediate matching node before current context node. So it wont see any node, it will return all immediate matching node.


6. Preceding Sibling Axes

Its return the all node which is immediately match before context node under same parent node (Return sibling node only)

Ex:

URL = http://www.w3schools.com/xml/default.asp

Xpath=//a[contains(.,'XML Namespaces')]/preceding-sibling::a

Output =  Returns 8 immediate matching node before current context node. So it will see the parent node (<div>) and returns the only matching sibling nodes.

No comments:

Post a Comment