[Java] Introduction to XPath

node

In XPath, there are seven types of nodes:

  • element
  • Attributes
  • text
  • Namespaces
  • Processing instructions
  • Comment
  • Document (root) node

XML documents are treated as a tree of nodes, and the root of the tree is called the document node or root node.

Insert picture description here

  • <bookstore> (Document node)

  • <author>J K. Rowling</author> (Element node)

  • lang="en" (Attribute node)

Two concepts:

  • The basic value (or atomic value, Atomic value) has
    no parent or no child node.

    J K. Rowling
    "en"
    
  • Item
    items are basic values ​​or nodes


grammar

Insert picture description here

Select node

XPath uses path expressions to select nodes in an XML document. Nodes are selected by following a path or step. The most useful path expressions are listed below:

expression description
nodename Select all child nodes of this node.
/ Pick from the root node.
// Select 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 attributes.

In the table below, we have listed some path expressions and their results:

Path expression result
bookstore Select 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 Select all book elements that are child elements of bookstore.
//book Select all book sub-elements, regardless of their position in the document.
bookstore//book Select all book elements that are descendants of the bookstore element, regardless of where they are located under the bookstore.
// @ lang Select all attributes named lang.

Predicates

The predicate is used to find a specific node or a node containing a specified value.

The predicate is embedded in square brackets.

In the following table, we list some path expressions with predicates and the results of the expressions:

Path expression result
/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 first two book elements that are child elements of the bookstore element.
// title [@lang] Select all title elements that have an attribute named lang.
// title [@ lang = 'eng'] Select all title elements, and these elements have a lang attribute with a value of eng.
/bookstore/book[price>35.00] Select all 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 Select 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 wildcards can be used to select unknown XML elements.

Wildcard description
* Match any element node.
@* Match any attribute node.
node() Match any type of node.

In the following table, we list some path expressions and the results of these expressions:

Path expression result
/bookstore/* Select all child elements of the bookstore element
//* Select all elements in the document.
//title[@*] Select all title elements with attributes.

Select several paths

By using the "|" operator in the path expression, you can select several paths.

In the following table, we list some path expressions and the results of these expressions:

Path expression result
//book/title | //book/price Select all the title and price elements of the book element.
//title | //price Select all the title and price elements in the document.
/bookstore/book/title | //price Select all the title elements of the book element belonging to the bookstore element and all the price elements in the document.

Axes

Insert picture description here
The axis can define a node set relative to the current node.

Axis name result
ancestor Select all ancestors (parents, grandfathers, etc.) of the current node.
ancestor-or-self Select all ancestors (parents, grandfathers, etc.) of the current node and the current node itself.
attribute 选取当前节点的所有属性。
child 选取当前节点的所有子元素。
descendant 选取当前节点的所有后代元素(子、孙等)。
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
following 选取文档中当前节点的结束标签之后的所有节点。
following-sibling 选取当前节点之后的所有兄弟节点
namespace 选取当前节点的所有命名空间节点。
parent 选取当前节点的父节点。
preceding 选取文档中当前节点的开始标签之前的所有节点。
preceding-sibling 选取当前节点之前的所有同级节点。
self 选取当前节点。

运算符

XPath 表达式可返回节点集、字符串、逻辑值以及数字。

运算符 描述 实例 返回值
| 计算两个节点集 //book | //cd 返回所有拥有 book 和 cd 元素的节点集
+ 加法 6 + 4 10
- 减法 6 - 4 2
* 乘法 6 * 4 24
div 除法 8 div 4 2
= 等于 price=9.80 如果 price 是 9.80,则返回 true。 如果 price 是 9.90,则返回 false。
!= 不等于 price!=9.80 如果 price 是 9.90,则返回 true。 如果 price 是 9.80,则返回 false。
< 小于 price<9.80 如果 price 是 9.00,则返回 true。 如果 price 是 9.90,则返回 false。
<= 小于或等于 price<=9.80 如果 price 是 9.00,则返回 true。如果 price 是 9.90,则返回 false。
> 大于 price>9.80 如果 price 是 9.90,则返回 true。 如果 price 是 9.80,则返回 false。
>= 大于或等于 price>=9.80 如果 price 是 9.90,则返回 true。 如果 price 是 9.70,则返回 false。
or price=9.80 or price=9.70 如果 price 是 9.80,则返回 true。 如果 price 是 9.50,则返回 false。
and versus price>9.00 and price<9.90 If price is 9.80, return true. If price is 8.50, false is returned.
mod Calculate the remainder of division 5 mod 2 1

Guess you like

Origin blog.csdn.net/weixin_45468845/article/details/108657663