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, selecting nodes
XPath uses a path expression to select a node in an XML document, and the node is selected along a path or step.
Common Path Expressions:
An expression |
Describe |
NodeName |
Selects all child nodes of the current node |
/ |
Select from the root node |
// |
Selects 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 /p> |
result |
Bookstore |
Select all child nodes of the bookstore element |
/bookstore |
Select root element bookstore |
Bookstore/book |
The Selects all child elements named book under Bookstore . |
//book |
Select all book child elements, regardless of their position in the document. |
Bookstore//book |
Select All Bookstore under the name book descendant elements , regardless of where they are located under bookstore. |
// @ lang |
Select All properties named Lang. |
2. Predicate (predicates)
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] |
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 '] |
Select all title elements that require these elements to have the Lang attribute value of Eng. |
/BOOKSTORE/BOOK[PRICE>35.00] |
Select the book element for all bookstore elements, which requires that the value of the child element of the book element, the price element, be greater than 35.00. |
/bookstore/book[price>35.00]/title |
Selects the title element of the book element in all bookstore elements, requiring that the value of the child element of the book element price element be greater than 35.00 |
3. Select Unknown node
XPath wildcard characters can be used to select unknown XML elements.
Wildcard characters |
Describe |
* |
Match any element node |
@* |
Match any attribute node |
Node () |
Match any type of node |
Instance
Path expression |
Results |
/bookstore/* |
Selects all child nodes of the bookstore element |
//* |
Select all elements in a document |
Title[@*] |
Select all the title elements with attributes. |
4. Select a number of paths
By using the '|' in a path expression operator, you can select a number of paths.
Instance
Path expression |
Results |
Book/title | //book/price |
Selects 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 all the title elements of the book element 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 |
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 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 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. |
6. Path
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:
- Relative position path:
/step/step/...
step/step/...
Each step is calculated based on the nodes in the current node set.
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 more deeply
syntax for step : axis Name:: node test [predicate]
Instance
Example |
Results |
Child::book |
Select all 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 properties of the current node |
Child::text () |
Selects all the text subnodes 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 (false so that the node is the book node) |
Child::*/child::p |
Selects all the price sun for the current node. |
7. XPath operator
Operator |
Describe |
Instance |
return value |
| |
Compute two node sets |
Book | Cd |
Returns all node sets with the 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, the Fasle is returned. |
!= |
Not equal to |
price!=9.80 |
Returns true if Price is 9.90. If Price is 9.80, the Fasle is returned. |
< |
Less than |
price<9.80 |
Returns true if Price is 9.00. If Price is 9.90, the Fasle is returned. |
<= |
Less than or equal to |
price<=9.80 |
Returns true if Price is 9.00. If Price is 9.90, the Fasle is returned. |
> |
Greater than |
price>9.80 |
Returns true if Price is 9.90. If Price is 9.80, the Fasle is returned. |
>= |
Greater than or equal to |
price>=9.80 |
Returns true if Price is 9.90. If Price is 9.70, the Fasle is returned. |
Or |
Or |
price=9.80 or price=9.70 |
Returns true if Price is 9.80. If Price is 9.50, the Fasle is returned. |
and |
And |
price>9.00 and price<9.90 |
Returns true if Price is 9.80. If Price is 8.50, the Fasle is returned. |
MoD |
Calculate the remainder of a division |
5 MoD 2 |
1 |
Finding the specified node through XPath