XPath uses a path expression to pick a node or set of nodes in an XML document. A node is picked up either along a path or a step (steps).
We will use this XML document in the following example.
<?xml version= "1.0" encoding= "iso-8859-1"?><bookstore><book> <title lang= "Eng" >Harry potter</title> <price>29.99</price></book><book> <title lang= "eng" > Learning xml</title> <price>39.95</price></book></bookstore>
The most useful path expressions are listed below:
expression |
|
nodename |
Select all child nodes of this node. |
/ | class= xl64 "width=" "346" > selected from the root node.
// |
Select the section in the document from the current node that matches the selection Points, regardless of their location. |
Select the current node. |
Select the parent node of the current node. |
@ |
select Properties. |
In the table below, we have listed some path expressions and the results of the expressions:
Instance
In the table below, we have listed some path expressions and the results of the expressions:
Path Expression |
Results |
Bookstore |
Selects all child nodes of the bookstore element. |
/bookstore |
Select the root element bookstore. Note: If the path starts with a forward slash (/), this path always represents the absolute path to an element! |
Bookstore/book |
Selects all book elements that belong to a child element of bookstore. |
Book |
Selects all book child elements, regardless of their position in the document. |
Bookstore//book |
Selects all book elements that belong to descendants of the bookstore element, regardless of where they are located under bookstore. |
@lang |
Select all attributes that are named Lang. |
predicate (predicates)
To find a particular node or a node that contains a specified value.
The predicate is embedded in square brackets.
Example
In the table below, we list some path expressions with predicates, as well as the results of expressions:
path expression |
result |
/bo OKSTORE/BOOK[1] |
selects the first book element that belongs to the bookstore child element. |
/bookstore/book[last ()] |
selects the last book element that belongs to the bookstore child element. |
/bookstore/book[last ()-1] |
selects the penultimate book element that belongs to the bookstore child element. |
/bookstore/book[position () <3] |
selects the first two book elements that belong to the child elements of the bookstore element. |
//title[@lang] |
selects all the title elements that have properties named Lang. |
//title[@lang = ' eng '] |
selects all title elements that have the Lang attribute value of Eng. |
/bookstore/book[price>35.00] |
selects all book elements of the bookstore element, and the value of the price element must be greater than 35.0 0. |
/bookstore/book[price>35.00]/title |
selects all the title elements of the book element in the bookstore element, where p The value of the rice element must be greater than 35.00. |
Select Unknown node
XPath wildcard characters can be used to select unknown XML elements.
wildcard characters |
Description |
* |
Matches any element node. |
@* |
matches any attribute node. |
Node () |
Matches any type of node. |
Instance
In the table below, we list some path expressions and the results of these expressions:
Path Expression |
Results |
/bookstore/* |
Selects all child elements of the bookstore element. |
//* |
Selects all elements in the document. |
Title[@*] |
Select all the title elements with attributes. |
Select several paths
By using the ' | ' in a path expression operator, you can select a number of paths.
Example
In the table below, we list some path expressions and the results of these expressions:
Path Expression |
Results |
Book/title | Book/price |
Selects all the title and price elements of the book element. |
Title | Price |
Selects all the title and price elements in the document. |
/bookstore/book/title | Price |
Selects all the title elements of the book element that belong to the bookstore element, and all the price elements in the document. |
"Selenium" XPath syntax (i)