Home > Article > Backend Development > XPath 11 instances
Example 1
The basic XPath syntax is similar to locating files in a file system. If the path starts with a slash /, then the path represents the 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 sub-elements of AAA
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA> /AAA/DDD/BBB
Select all the sub-elements DDD of AAA Child element
<AAA> <BBB/> <CCC/> <BBB/> <BBB/> <DDD> <BBB/> </DDD> <CCC/> </AAA>
Example 2
If the path starts with a double slash //, it means that all elements in the document that meet 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
asterisk * means to select all elements located by the path before the asterisk
/AAA/CCC/DDD/*
Select all elements whose paths are 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 with 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 be further specified 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 AAA element A BBB child element
<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 those with name attributes BBB element
<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 id name and its value is \'bbb\' ''s BBB element
<AAA> <BBB id = \"b1\"/> <BBB name = \" bbb \"/> <BBB name = \"bbb\"/> </AAA> //BBB[normalize-space(@name)=\'bbb\']Example 7
count() function can count the number of selected elements
//*[count(BBB)=2 ]
Select an element with 2 BBB sub-elements
<AAA> <BBB id = \"b1\"/> <BBB name = \" bbb \"/> <BBB name = \"bbb\"/> </AAA>
Select an element with 2 sub-elements
<AAA> <CCC> <BBB/> <BBB/> <BBB/> </CCC> <DDD> <BBB/> <BBB/> </DDD> <EEE> <CCC/> <DDD/> </EEE> </AAA> //*[count(*)=2]Select an element with 3 sub-elements
<AAA> <CCC> <BBB/> <BBB/> <BBB/> </CCC> <DDD> <BBB/> <BBB/> </DDD> <EEE> <CCC/> <DDD/> </EEE> </AAA> //*[count(*)=3]
Example 8
The name() function returns the name of the element, and the start-with() function uses the first parameter string of the function to start with the second parameter character. Returns true. The contains() function returns true when its first string parameter contains the second string parameter.
//*[name()=\'BBB\']
Select all elements whose names are BBB (here equivalent to //BBB)
<AAA> <CCC> <BBB/> <BBB/> <BBB/> </CCC> <DDD> <BBB/> <BBB/> </DDD> <EEE> <CCC/> <DDD/> </EEE> </AAA>
Select all elements whose names start with \"B\"
<AAA> <BCC> <BBB/> <BBB/> <BBB/> </BCC> <DDB> <BBB/> <BBB/> </DDB> <BEC> <CCC/> <DBD/> </BEC> </AAA> //*[starts-with(name(),\'B\')]
Select all elements whose names contain \"C\"
<AAA> <BCC> <BBB/> <BBB/> <BBB/> </BCC> <DDB> <BBB/> <BBB/> </DDB> <BEC> <CCC/> <DBD/> </BEC> </AAA> //*[contains(name(),\'C\')]
Example 9
Multiple paths can be merged together using the separator |
#//CCC | //BBB
<AAA> <BCC> <BBB/> <BBB/> <BBB/> </BCC> <DDB> <BBB/> <BBB/> </DDB> <BEC> <CCC/> <DBD/> </BEC> </AAA>
<AAA> <BBB/> <CCC/> <DDD> <CCC/> </DDD> <EEE/> </AAA> /AAA/EEE | //BBB
<AAA> <BBB/> <CCC/> <DDD> <CCC/> </DDD> <EEE/> </AAA> /AAA/EEE | //DDD/CCC | /AAA | //BBB
/AAA
<AAA> <BBB/> <CCC/> <DDD> <CCC/> </DDD> <EEE/> </AAA>
<AAA> <BBB/> <CCC/> </AAA> /child::AAA
is equivalent to /child::AAA/child::BBB
<AAA> <BBB/> <CCC/> </AAA>
/child::AAA/child: :BBB
is equivalent to /AAA/BBB
<AAA> <BBB/> <CCC/> </AAA>/child::AAA/BBB
Both can be merged
<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 include attribute and namespace nodes.
/descendant::*
Select all descendants of the document root element. That is, all elements are selected
<AAA> <BBB/> <CCC/> </AAA>
<AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB> <CCC> <DDD> <EEE> <DDD> <FFF/> </DDD> </EEE> </DDD> </CCC> </AAA> /AAA/BBB/descendant::*
<AAA> <BBB> <DDD> <CCC> <DDD/> <EEE/> </CCC> </DDD> </BBB> <CCC> <DDD> <EEE> <DDD> <FFF/> </DDD> </EEE> </DDD> </CCC> </AAA> //CCC/descendant::*