Example 1
Basic XPath syntax is similar to locating files in a file system. If the path starts with a slash /, then the path represents an absolute path to an element.
/AAA
Select the root element AAA
<AAA>
<BBB/>
<CCC/>
<BBB/>
<BBB/>
<DDD>
<BBB/>
</DDD>
<CCC/>
</AAA>
/AAA/CCC
Select all CCC child elements of AAA
<AAA>
<BBB/>
<CCC/>
<BBB/>
<BBB/>
<DDD>
<BBB/>
</DDD>
<CCC/>
</AAA>
/AAA/DDD/BBB
Selects all child elements of DDD, a child element of AAA
<AAA>
<BBB/>
<CCC/>
<BBB/>
<BBB/>
<DDD>
<BBB/>
</DDD>
<CCC/>
</AAA>
Example 2
If the path begins with a double slash //, it means that all elements in the document that satisfy the rules after the double slash // are selected (regardless of hierarchical relationship) //BBB
Select all BBB elements
<AAA>
<BBB/>
<CCC/>
<BBB/>
<DDD>
<BBB/>
</DDD>
<CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
</CCC>
</AAA>
//DDD/BBB
Select all BBB elements whose parent element is DDD
<AAA>
<BBB/>
<CCC/>
<BBB/>
<DDD>
<BBB/>
</DDD>
<CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
</CCC>
</AAA>
Example 3
The asterisk* means to select all elements located by the path before the asterisk
/AAA/CCC/DDD/*
Select all elements with paths attached to /AAA/CCC/DDD
<AAA>
<XXX>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
</DDD>
</XXX>
<CCC>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
</DDD>
</CCC>
<CCC>
<BBB>
<BBB>
<BBB/>
</BBB>
</BBB>
</CCC>
</AAA>
/*/*/*/BBB
Select all BBB elements that have 3 ancestor elements
<AAA>
<XXX>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
</DDD>
</XXX>
<CCC>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
</DDD>
</CCC>
<CCC>
<BBB>
<BBB>
<BBB/>
</BBB>
</BBB>
</CCC>
</AAA>
//*
Select all elements
<AAA>
<XXX>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
</DDD>
</XXX>
<CCC>
<DDD>
<BBB/>
<BBB/>
<EEE/>
<FFF/>
</DDD>
</CCC>
<CCC>
<BBB>
<BBB>
<BBB/>
</BBB>
</BBB>
</CCC>
</AAA>
Example 4
The expression in the square number can further specify the element, where the number represents the position of the element in the selection set, and the last() function represents the last element in the selection set.
/AAA/BBB[1]
Select the first BBB child element of AAA
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
/AAA/BBB[last()]
Select the last BBB child element of AAA
<AAA>
<BBB/>
<BBB/>
<BBB/>
<BBB/>
</AAA>
Example 5
//@id
Select all id attributes
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
</AAA>
//BBB[@id]
Select BBB elements with id attributes
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
</AAA>
//BBB[@name]
Select the BBB element with name attribute
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
</AAA>
//BBB[@*]
Select BBB elements with any attributes
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
</AAA>
//BBB[not(@*)]
Select BBB elements without attributes
<AAA>
<BBB id = "b1"/>
<BBB id = "b2"/>
<BBB name = "bbb"/>
<BBB/>
</AAA>
Example 6
The value of the attribute can be used as a selection criterion. The normalize-space function removes leading and trailing spaces, and replaces consecutive strings of spaces with a single space
//BBB[@id='b1']
Select the BBB element that contains the attribute id and its value is 'b1'
<AAA>
<BBB id = "b1"/>
<BBB name = " bbb "/>
<BBB name = "bbb"/>
</AAA>
//BBB[@name='bbb']
Select the BBB element that contains the attribute name and its value is 'bbb'
<AAA>
<BBB id = "b1"/>
<BBB name = " bbb "/>
<BBB name = "bbb"/>
</AAA>
//BBB[normalize-space(@name)='bbb']
Select the BBB element that contains the attribute name and its value (after removing the leading and trailing spaces using the normalize-space function) is 'bbb'
<AAA>
<BBB id = "b1"/>
<BBB name = " bbb "/>
<BBB name = "bbb"/>
</AAA>
Example 7
The count() function can count the number of selected elements
//*[count(BBB)=2]
Select elements with 2 BBB child elements
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
</CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
<EEE>
<CCC/>
<DDD/>
</EEE>
</AAA>
//*[count(*)=2]
Select an element with 2 children
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
</CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
<EEE>
<CCC/>
<DDD/>
</EEE>
</AAA>
//*[count(*)=3]
Select an element with 3 child elements
<AAA>
<CCC>
<BBB/>
<BBB/>
<BBB/>
</CCC>
<DDD>
<BBB/>
<BBB/>
</DDD>
<EEE>
<CCC/>
<DDD/>
</EEE>
</AAA>
Example 8
The name() function returns the name of the element, the start-with() function returns true when the first parameter string of the function starts with the second parameter character, and the contains() function returns true when its first string parameter Returns true when the second string parameter is included.
//*[name()='BBB']
Select all elements named BBB (here equivalent to //BBB)
<AAA>
<BCC>
<BBB/>
<BBB/>
<BBB/>
</BCC>
<DDB>
<BBB/>
<BBB/>
</DDB>
<BEC>
<CCC/>
<DBD/>
</BEC>
</AAA>
//*[starts-with(name(),'B')]
Select all elements whose names start with "B"
<AAA>
<BCC>
<BBB/>
<BBB/>
<BBB/>
</BCC>
<DDB>
<BBB/>
<BBB/>
</DDB>
<BEC>
<CCC/>
<DBD/>
</BEC>
</AAA>
//*[contains(name(),'C')]
Select all elements whose names contain "C"
<AAA>
<BCC>
<BBB/>
<BBB/>
<BBB/>
</BCC>
<DDB>
<BBB/>
<BBB/>
</DDB>
<BEC>
<CCC/>
<DBD/>
</BEC>
</AAA>
Example 9
Multiple paths can be combined together using the delimiter |
//CCC | //BBB
Select all CCC and BBB elements
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
<EEE/>
</AAA>
/AAA/EEE | //BBB
Select all BBB elements and all EEE elements that are children of AAA
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
<EEE/>
</AAA>
/AAA/EEE | //DDD/CCC | /AAA | //BBB
There is no limit to the number of paths that can be merged
<AAA>
<BBB/>
<CCC/>
<DDD>
<CCC/>
</DDD>
<EEE/>
</AAA>
Example 10
The child axis contains the child elements of the context node. As the default axis, it can be ignored.
/AAA
Equivalent to /child::AAA
<AAA>
<BBB/>
<CCC/>
</AAA>
/child::AAA
Equivalent to /AAA
<AAA>
<BBB/>
<CCC/>
</AAA>
/AAA/BBB
Equivalent to /child::AAA/child::BBB
<AAA>
<BBB/>
<CCC/>
</AAA>
/child::AAA/child::BBB
Equivalent to /AAA/BBB
<AAA>
<BBB/>
<CCC/>
</AAA>
/child::AAA/BBB
Both can be combined
<AAA>
<BBB/>
<CCC/>
</AAA>
Example 11
The descendant axis contains the descendants of the context node. A descendant refers to a child node or a child node of a child node, etc., so the descendant axis does not contain attribute and namespace nodes.
/descendant::*
Selects all descendants of the document root element. That is, all elements are selected
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>
/AAA/BBB/descendant::*
Selects all descendant elements of /AAA/BBB
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
<CCC>
<DDD>
<EEE>
<DDD>
<FFF/>
</DDD>
</EEE>
</DDD>
</CCC>
</AAA>
//CCC/descendant::*
Select all elements that have CCC among their ancestors
<AAA>
<BBB>
<DDD>
<CCC>
<DDD/>
<EEE/>
</CCC>
</DDD>
</BBB>
</AAA>