In c#, we often search and traverse nodes, this is what we can use XPath syntax, Example Xml:
<?xml version="1.0" encoding="utf-8" ?>
<pets>
<cat color="black" weight="10">
<price>100</price>
<desc>this is a black cat</desc>
</cat>
<cat color="white" weight="9">
<price>80</price>
<desc>this is a white cat</desc>
</cat>
<cat color="yellow" weight="15">
<price>80</price>
<desc>this is a yellow cat</desc>
</cat>
<dog color="black" weight="10">
<price>100</price>
<desc>this is a black dog</desc>
</dog>
<dog color="white" weight="9">
<price>80</price>
<desc>this is a white dog</desc>
</dog>
<dog color="yellow" weight="15">
<price>80</price>
<desc>this is a yellow dog</desc>
</dog>
</pets>
Copy after login
XPath syntax:
1. Symbols in XPath
Symbol Description
Example
Example description
/
means selecting from the root node
/pets
Select the root node pets
Represents the spacer between the node and the child node
/pets/dog
Select the dog node under the pets node
//xx
means searching from the entire xml document, regardless of the current node position
//price
Select all price nodes in the document
.
Single English A half-width period indicates selecting the current node
/pets/.
Selecting the pets node
..
##Double dot indicates selecting the parent node
/pets/dog[0 ]/..
represents the pets node, which is the parent node of the first dog node
# #@xx
represents the selection attribute
//dog/@color
Indicates selecting the color attribute set of all dog nodes
[…]
中The brackets indicate the selection conditions, and the conditions inside the brackets are
//dog[@color='white']
All dogs with the color white Node
//dog[/price<100]
All dogs whose price byte point value is less than 100 Node
The number in the square brackets is the node index, similar to the array in languages such as C#, the array subscript starts from 1
//dog[1]
The first dog node
//dog[ last()]
The last dog node, last() is the xPath built-in function
##|
The single vertical bar indicates the combination of merged nodes
//dog[@color='white'] | //cat[ @color='white']
The dog node whose color attribute is white and the cat node whose color attribute is white
*
The asterisk represents a node or attribute with any name
//dog/*
Represents all child nodes of the dog node
//dog/@*
Represents all attribute nodes of the dog node
2. XPath mathematical operators
+ The plus sign means adding
- means subtracting numbers
* means multiplying by
p means dividing by, where the mathematical division sign / has been used as a separator between nodes
mod means remainder
3. XPath logical operator
= equals, equivalent to ==
# in c
!= Not equal to
> Greater than
>= Greater than or equal to
< Less than
<= Less than or equal to
and and with the relationship
or or or the relationship
4. XPath Axes Literally translated this is XPath The meaning of axis, but according to my understanding, it is more appropriate to translate it into XPath node relationship operation keyword, which is a set of keywords plus :: double colon to indicate a node or a group of nodes that are related to the current node.
Use syntax: axisname::nodetest[predicate] That is, axis name::node name [get node condition]
The specific description is as follows:
Keywords
Description
Example
Example description
ancestor
The parent node of the current node
ancestor:: pig
The pig node among the ancestor nodes of the current node
##ancestor-or-self
Current node and its parent node
ancestor::pig
attribute
All attributes of the current node
attribute::weight
##Quite For @weight, attribute:: and @ are equivalent
child
All bytes of the current node Click
child::*[name()!='price']
Select the child node whose name is not price
descendant
Descendant Node
descendant::*[@*]
Descendant nodes with attributes
descendant-or-self
Descendant nodes and current nodes
descendant-or-self::*
following
All nodes after the current node in the Xml document
following::*
following-sibling
The same father and younger brother node of the current node
following-sibling: :
preceding
All nodes before the current node in the Xml document
preceding::*
##namespace
Select all namespace nodes of the current node
namespace::*
parent
The parent node of the current node
parent::
is equivalent to a double point..
preceding-sibling
The same father and brother node after the current node
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn