XPath:- Xpath is
used to locate a web element based on its XML path. XML stands for Extensible
Mark-up Language and is used to store, organize and transport arbitrary data.
It stores data in a key-value pair which is very much similar to HTML tags.
XPath
Axes: - Plays
a major role to handle dynamic web elements.
·
Axis
details: -
AXIS Name
|
Result
|
ancestor
|
Selects all ancestors (parent, grandparent, etc.) of the current node
|
ancestor-or-self
|
Selects all ancestors (parent, grandparent, etc.) of the current node
and the current node itself
|
attribute
|
Selects all attributes of the current node
|
child
|
Selects all children of the current node
|
descendant
|
Selects all descendants (children, grandchildren, etc.) of the current
node
|
descendant-or-self
|
Selects all descendants (children, grandchildren, etc.) of the current
node and the current node itself
|
following
|
Selects everything in the document after the closing tag of the
current node
|
following-sibling
|
Selects all siblings after the current node
|
namespace
|
Selects all namespace nodes of the current node
|
parent
|
Selects the parent of the current node
|
preceding
|
Selects all nodes that appear before the current node in the document,
except ancestors, attribute nodes and namespace nodes
|
preceding-sibling
|
Selects all siblings before the current node
|
self
|
Selects the current node
|
·
Syntax
-
axisname
|
::
|
nodetest
|
[predicate]
|
an axis
|
symbol
|
a node-test
|
zero or more predicates
|
Defines the tree-relationship between the selected nodes and the
current node
|
Constant
|
Identifies a node within an axis
|
To further drill down the selected node-set
|
·
Examples
of axis usage: -
Example
|
Result
|
child::book
|
Selects all book nodes that are children of the current node
|
attribute::lang
|
Selects the lang attribute of the current node
|
child::*
|
Selects all element children of the current node
|
attribute::*
|
Selects all attributes of the current node
|
child::text()
|
Selects all text node children of the current node
|
child::node()
|
Selects all children of the current node
|
descendant::book
|
Selects all book descendants of the current node
|
ancestor::book
|
Selects all book ancestors of the current node
|
ancestor-or-self::book
|
Selects all book ancestors of the current node - and the current as
well if it is a book node
|
child::*/child::price
|
Selects all price grandchildren of the current node
|
·
Xpath
example with Axis: -I have given few
examples. Please try to use other axes with your own. If you are facing any
issues, please let me know.
AXIS Names
|
XML
|
Xpath
|
ancestor
|
<root>
<foo> <bar attr="xxx"></bar> </foo> <foo> <bar attr="val"></bar> </foo> <foo> <bar attr="zzz"></bar> </foo> </root> |
//*[ancestor::foo[bar[@attr="val"]]]
or root/foo/bar[ancestor::foo[bar[@attr="val"]]]
|
ancestor-or-self
|
<root>
<item key="a"> <item key="b"> <item key="c"> <item key="d"/> </item> </item> <item key="e"> <item key="f"> <item key="g"/> </item> </item> </item> </root> |
ancestor-or-self::*[parent::item[@key='a']]
|
attribute
|
<Employees>
<Employee id="3"> <age>40</age> <name>Tom</name> <gender>Male</gender> <role>Manager</role> </Employee> <Employee id="4"> <age>25</age> <name>Meghna</name> <gender>Female</gender> <role>Manager</role> </Employee> </Employees> |
//Employee[@id='4'] or
/Employees/Employee[@id='4']
|
child
|
child::* selects all element children
of the context node
child::text() selects all text node children of the context node child::node() selects all the children of the context node, whatever their node type |
|
descendant
|
<div id="books">
<table> <tr><td class="title">Lord of the Rings</td><td class="author">JRR Tolkein</td></tr> <tr><td class="title">The Hitch-Hikers Guide to the Galaxy</td><td class="author">Douglas Adams</td></tr> </table> </div> |
//id('books')//td[@class='title']
or //id('books')//td[@class='title'] |
No comments:
Post a Comment