XPath Expression
XPath location path expression is a specialize XPath expression to identifies XPath nodes relative to a current context node. location path is like a Linux, Window, Mac file explorer address path.
XPath Location path expression selects a set of nodes relative to the context node. XPath expression evaluate under XPath rules to a context.
Type of XPath location path expression,
XPath Expression (Location Path with Examples)
Following XML document is our experimental document,
<?xml version="1.0" standalone="yes"?>
<empinfo>
<employee id="1">
<name>Opal Kole</name>
<designation discipline="web" experience="3 year">Senior Engineer</designation>
<email>[email protected]</email>
</employee>
<employee id="2">
<name from="CA">Max Miller</name>
<designation discipline="DBA" experience="2 year">DBA Engineer</designation>
<email>[email protected]</email>
</employee>
<employee id="3">
<name>Beccaa Moss</name>
<designation discipline="appdev">Application Developer</designation>
<email>[email protected]</email>
</employee>
</empinfo>
XPath Common Identifier Expression For Finding XPath Node (Wildcards)
Following table represent common XPath location identifiers,
Expression | Description |
---|---|
. | Referring to a context node. |
.. | Referring to a parent context node. |
/ | Referring to a root node. |
// | Referring to a node anywhere within document. |
* | Referring to all elements that are child of context node. |
| or |
Referring to a OR condition combine expression either first expression or second expression. |
and | Referring to a AND condition combine expression. |
text() | Select all text node children of current element. |
XPath Element Expression For Finding XPath Node
Element location paths you can define either on relative or absolute.
Relative location path is sequence of child element separated by "/" (forward slash).
element_name/element_name
Absolute location path is beginning to a "/" (forward slash) with root element and after sequence of child element separated by "/" (forward slash).
/root_element/element_name/element_name
Following describing XPath element expressions for locating XPath nodes.
Expression | Description |
---|---|
empinfo | Direct child element Select empinfo element all children nodes. |
empinfo/employee | Child of child element Select employee node of the empinfo element. |
empinfo/employee/name | Sequence of child of child element Select name element of employee element of empinfo element. |
/ | Root node |
/empinfo | All child elements of root element Select all child elements of empinfo element. |
/empinfo/employee | Child of root element Select all child element of empinfo root element. |
/empinfo/employee/name | Sequence of child of root element Select name element of employee element of empinfo element. |
*/employee | Select all grand parent element Select all employee grand parent elements. |
// | Descendant element |
empinfo//employee | Descendant of element Select all employee element of empinfo element. |
/empinfo//employee | Descendant of root element Select all employee element in XML document within context node empinfo root element. |
//employee/name | Child of descendant element Select all name element in XML document within context node employee element. |
XPath Attribute Expression For Finding XPath Node
XPath give you control to create attribute expression for locating node in XML document.
Expression | Description |
---|---|
@ | Attribute selector identifier |
@discipline | select discipline attributes of the context. |
//employee/@id | Element attribute Select employee elements with id attribute within XML document. |
/empinfo/employee/@id | Element attribute of the root element Select employee elements with id attribute of the empinfo root element. |
@* | all attribute of the context node |
//@* | All attribute within XML document |
//@id | id attribute within XML document |
XPath Predicates Identifier For Finding XPath Node
XPath predicates identifier return boolean result either True or False.
you can use any relation operators <, >, <=, >=, =, != and boolean operator or, and.
Expression | Description |
---|---|
element_name[N] | Opening and closing square bracket referring to a specific element sequence number (array sequence). |
empinfo/employee[2] | Second Child of child element Select second employee of empinfo element. |
empinfo/employee[2]/name | Sequence of child of child element Select name element of second employee element of empinfo element. |
element_name[@attribute_name] empinfo/employee[@id] |
Select element with attribute Select all employee element with given id attribute. |
element_name[@attribute_name=value] empinfo/employee[@id=2] |
Select element with specified attribute value Finding employee elements with given attribute and value. Matching only those elements whose attribute and value are specified in XPath expression. |
empinfo/employee[@id=2][2] | Select all employee element of given attribute and value. At last select only second employee element. |
empinfo/employee[1][@id=1] | Select first employee element with given attribute and value. |
//designation[@discipline and @experience] | Selects all the designation element with given first and second both attribute. |
//designation[@discipline or @experience] //designation[@discipline | @experience] |
Select all the designation element with given either first or second, both attribute. |