using XPath to find the need to introduce Jaxen-xx-xx.jar, otherwise it will report java.lang.noclassdeffounderror:org/jaxen/jaxenexception exception.
List list=document. selectnodes ("/books/book/@show"); XPath Syntax 1, select the node
XPath uses a path expression to select a node in an XML document, and the node is selected along the path or step.
Common Path Expressions:
An expression |
Description |
NodeName |
Select all child nodes of the current node |
/ |
Selecting from the root node |
// |
Select the nodes in the document from the current node of the matching selection, regardless of their location |
. |
Select the current node |
.. |
Select the parent node of the current node |
@ |
Select Properties |
Instance
Path Expression |
Results |
Bookstore |
Select all child nodes of the bookstore element |
/bookstore |
Select the root element bookstore |
Bookstore/book |
Select all child elements with the name book under Bookstore. |
Book |
Selects all book child elements, regardless of their position in the document. |
Bookstore//book |
Select all descendant elements under bookstore whose name is book, regardless of where they are located under bookstore. |
@Lang |
Select All properties named Lang. |
2. Predicate (predicates)
predicate pragmatics to find a particular node or a node that contains a specified value.
The predicate is embedded in square brackets.
Instance
Some path expressions for common predicates:
Path Expression |
Results |
/BOOKSTORE/BOOK[1] |
Select the first book element that belongs to the bookstore child element. |
/bookstore/book[last ()] |
Select the last book element that belongs to the bookstore child element. |
/bookstore/book[last ()-1] |
Select the penultimate book element that belongs to the bookstore child element. |
/bookstore/book[position () <3] |
Select The book element for the first two child elements that belong to the bookstore element. |
title[@lang] |
Select the title element for all properties that have the name Lang. |
title[@lang = ' Eng '] |
Select all the title elements and require that these elements have the lang attribute with the value eng. |
/BOOKSTORE/BOOK[PRICE>35.00] |
Selecting the book element for all bookstore elements requires the value of the child element price element of the book element to be greater than 35.00. |
/bookstore/book[price>35.00]/title |
Selects the title element of the book element in all bookstore elements, requiring the value of the child element price element of the book element to be greater than 35.00 |
3. Select Unknown node
XPath wildcard characters can be used to select an unknown XML element.
wildcard characters |
Description |
* |
Match any element node |
@* |
Match any attribute node |
Node () |
Match any type of node |
Instance
Path Expression |
Results |
/bookstore/* |
Select all child nodes of the bookstore element |
//* |
Select all elements in a document |
Title[@*] |
Select all the title elements with attributes. |
4. Select several paths
By using the "|" in a path expression operator, you can select several paths.
Instance
Path Expression |
Results |
Book/title | //book/price |
Select the title and price elements of all book elements. |
Title | //price |
Select the title and price elements in all documents. |
/bookstore/book/title| Price |
Select the title element of all book elements that belong to the bookstore element, and all the price elements in the document. |
5. XPath Axis
An axis defines a node set that is relative to the current node.
Axis name |
Results |
Ancestor |
Select all ancestors of the current node (parent, grandfather, etc.) |
Ancestor-or-self |
Select all ancestors of the current node (parent, grandfather, etc.) and the current node itself |
Attribute |
Select all properties of the current node |
Child |
Selects all child elements of the current node. |
Descendant |
Select all descendant elements (child, grandchild, etc.) for the current node. |
Descendant-or-self |
Selects all descendant elements (child, grandchild, etc.) 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 |
Select all namespace nodes for the current node |
Parent |
Select 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. |
6, the pathLocation path expression
The location path can be absolute or relative.
The absolute path starts with a forward slash (/), and the relative path does not. In both cases, the location path includes one or more steps, each of which is separated by a slash: the absolute position path: The relative location path:
/step/step/...
step/step/...
Each step is computed according to the nodes in the current node set. Steps (step) include:
Axis: Defines the tree relationship between the selected node and the current node
Node Test (node-test): Identifying a node within an axis
0 or more predicates (predicate): Refine the selected node set more deeply
The 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 attribute for the current node |
Child::* |
Select all child elements of the current node |
Attribute::* |
Select all properties of the current node |
Child::text () |
Select all text child nodes of the current node |
Child::node () |
Select all child nodes of the current node |
Descendant::book |
Select all book descendants of the current node |
Ancestor::book |
Selects all ancestors of the current node |
Ancestor-or-self::book |
Selects all ancestors of the current node and the current node (false so that the node is the book node) |
Child::*/child::p Rice |
Select all price grandchildren for the current node. |
7. XPath Operators
operator |
Description |
instance |
return value |
| |
Calculate two node sets |
Book | Cd |
Returns all node sets with book and CK 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, return Fasle. |
!= |
Not equal to |
price!=9.80 |
Returns true if Price is 9.90. If Price is 9.80, return Fasle. |
< |
Less than |
price<9.80 |
Returns true if Price is 9.00. If Price is 9.90, return Fasle. |
<= |
Less than or equal to |
price<=9.80 |
Returns true if Price is 9.00. If Price is 9.90, return Fasle. |
> |
Greater than |
price>9.80 |
Returns true if Price is 9.90. If Price is 9.80, return Fasle. |
>= |
Greater than or equal to |
price>=9.80 |
Returns true if Price is 9.90. If Price is 9.70, return Fasle. |
Or |
Or |
price=9.80 or price=9.70 |
Returns true if Price is 9.80. If Price is 9.50, return Fasle. |
and |
And |
price>9.00 and price<9.90 |
Returns true if Price is 9.80. If Price is 8.50, return Fasle. |
MoD |
Calculate the remainder of a division |
5 MoD 2 |
1 |