The specific learning of XPath can be viewed through the viewing (link: http://www.w3school.com.cn/xpath/index.asp)
Here is just the usual use of a few tables posted, later query:
Here's the XPath I personally feel like selectors that have
syntax +XPath axis operation + operator + function
Several combinations are used.
A lot of functions, do not post out, need to be able to search by the information: http://www.w3school.com.cn/xpath/xpath_functions.asp
XPath Syntax Selection node
XPath uses a path expression to select a node in the XML document. A node is selected by a path or step.
The most useful path expressions are listed below:
An expression |
Description |
NodeName |
Select all child nodes of this node. |
/ |
Select from the root node. |
// |
Selects the nodes in the document from the current node that matches the selection, regardless of their location. |
. |
Select the current node. |
.. |
Selects the parent node of the current node. |
@ |
Select the attribute. |
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.
Instance
In the table below, we list some path expressions with predicates, as well as the results of expressions:
Path Expression |
Results |
/BOOKSTORE/BOOK[1] |
Selects the first book element that belongs to a bookstore child element. |
/bookstore/book[last ()] |
Select the last book element that belongs to the bookstore child element. |
/bookstore/book[last ()-1] |
Select the second-to-last book element that belongs to the bookstore child element. |
/bookstore/book[position () <3] |
Select the first two book element that belongs to the child element of the bookstore element. |
title[@lang] |
Select all the title elements that have properties named Lang. |
title[@lang = ' Eng '] |
Selects all title elements, and these elements have the lang attribute value of Eng. |
/BOOKSTORE/BOOK[PRICE>35.00] |
Selects all the book elements of the bookstore element, and the value of the price element must be greater than 35.00. |
/bookstore/book[price>35.00]/title |
Selects all the title elements of the book element in the bookstore element, and the value of the price 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.
Instance
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. |
XPath Axes (axis) XPath axis
An axis defines a set of nodes relative to the current node.
Axis name |
Results |
Ancestor |
Selects all ancestors of the current node (parent, grandfather, etc.). |
Ancestor-or-self |
Selects all ancestors of the current node (parent, grandfather, etc.) and the current node itself. |
Attribute |
Selects all the properties of the current node. |
Child |
Selects all child elements of the current node. |
Descendant |
Selects all descendant elements (child, grandchild, and so on) of the current node. |
Descendant-or-self |
Selects all descendant elements (child, grandchild, and so on) of the current node and the current node itself. |
Following |
Selects all nodes after the end tag of the current node in the document. |
Namespace |
Selects all the namespace nodes of the current node. |
Parent |
Selects the parent node of the current node. |
Preceding |
Selects all nodes before the start tag of the current node in the document. |
Preceding-sibling |
Selects all sibling nodes before the current node. |
Self |
Select the current node. |
Position path expression
The location path can be absolute, or it can be relative.
The absolute path starts with a forward slash (/), and the relative path does not. In both cases, the location path consists of one or more steps, each of which is split by a slash:
Absolute location Path:
/step/step/...
Relative position path:
step/step/...
Each step is calculated based on the nodes in the current node set.
Step includes:
-
Axes (axis)
-
Defines the tree relationship between the selected node and the current node
-
Node Test (node-test)
-
Identify the nodes inside an axis
-
0 or more predicates (predicate)
-
refine the selected node set in more
detail
Syntax of the step:
Axis Name:: node test [predicate]
Instance
Example |
Results |
Child::book |
Select all the book nodes that belong to the child elements of the current node. |
Attribute::lang |
Select the Lang property of the current node. |
Child::* |
Selects all child elements of the current node. |
Attribute::* |
Selects all the properties of the current node. |
Child::text () |
Selects all the text child nodes of the current node. |
Child::node () |
Selects all child nodes of the current node. |
Descendant::book |
Selects all book descendants of the current node. |
Ancestor::book |
Selects all the book ancestors of the current node. |
Ancestor-or-self::book |
Selects all the book ancestors of the current node and the current node (if this node is a book node) |
Child::*/child::p |
Selects all the price sun nodes of the current node. |
XPath operator XPath operator
The following lists the operators that are available in XPath expressions:
operator |
Description |
Example |
return value |
| |
Compute two node sets |
Book | Cd |
Returns all node sets that have book and CD elements |
+ |
Addition |
6 + 4 |
10 |
- |
Subtraction |
6-4 |
2 |
* |
Multiplication |
6 * 4 |
24 |
Div |
Division |
8 Div 4 |
2 |
= |
Equals |
price=9.80 |
Returns true if Price is 9.80. If Price is 9.90, False is returned. |
!= |
Not equal to |
price!=9.80 |
Returns true if Price is 9.90. If Price is 9.80, False is returned. |
< |
Less than |
price<9.80 |
Returns true if Price is 9.00. If Price is 9.90, False is returned. |
<= |
Less than or equal to |
price<=9.80 |
Returns true if Price is 9.00. If Price is 9.90, False is returned. |
> |
Greater than |
price>9.80 |
Returns true if Price is 9.90. If Price is 9.80, False is returned. |
>= |
Greater than or equal to |
price>=9.80 |
Returns true if Price is 9.90. If Price is 9.70, False is returned. |
Or |
Or |
price=9.80 or price=9.70 |
Returns true if Price is 9.80. If Price is 9.50, False is returned. |
and |
And |
price>9.00 and price<9.90 |
Returns true if Price is 9.80. If Price is 8.50, False is returned. |
MoD |
Calculate the remainder of a division |
5 MoD 2 |
1 |
XPath syntax quick check