XPath 11 instances

黄舟
Release: 2017-02-28 16:40:03
Original
1508 people have browsed it

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
Copy after login

Select all CCC sub-elements of AAA

<AAA>           
<BBB/>           
<CCC/>           
<BBB/>           
<BBB/>           
<DDD>                
<BBB/>           
</DDD>           
<CCC/>      
</AAA>  
/AAA/DDD/BBB
Copy after login

Select all the sub-elements DDD of AAA Child element

  <AAA> 
          <BBB/> 
          <CCC/> 
          <BBB/> 
          <BBB/> 
          <DDD> 
               <BBB/> 
          </DDD> 
          <CCC/> 
     </AAA>
Copy after login

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
Copy after login

Select all BBB elements whose parent element is DDD

     <AAA> 
          <BBB/> 
          <CCC/> 
          <BBB/> 
          <DDD> 
               <BBB/> 
          </DDD> 
          <CCC> 
               <DDD> 
                    <BBB/> 
                    <BBB/> 
               </DDD> 
          </CCC> 
     </AAA>
Copy after login


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
Copy after login

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> 
 
//*
Copy after login

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>
Copy after login

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()]
Copy after login


Select the last BBB child element of AAA

    <AAA> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
          <BBB/> 
     </AAA>
Copy after login

Example 5


//@id
Select all id attributes

<AAA> 
          <BBB id = \"b1\"/> 
          <BBB id = \"b2\"/> 
          <BBB name = \"bbb\"/> 
          <BBB/> 
     </AAA> 
 
//BBB[@id]
Copy after login

Select BBB elements with id attributes

  <AAA> 
          <BBB id = \"b1\"/> 
          <BBB id = \"b2\"/> 
          <BBB name = \"bbb\"/> 
          <BBB/> 
     </AAA> 
 
//BBB[@name]
Copy after login

Select those with name attributes BBB element

<AAA> 
          <BBB id = \"b1\"/> 
          <BBB id = \"b2\"/> 
          <BBB name = \"bbb\"/> 
          <BBB/> 
     </AAA> 
 
//BBB[@*]
Copy after login

Select BBB elements with any attributes

<AAA> 
          <BBB id = \"b1\"/> 
          <BBB id = \"b2\"/> 
          <BBB name = \"bbb\"/> 
          <BBB/> 
     </AAA> 
 
//BBB[not(@*)]
Copy after login

Select BBB elements without attributes

<AAA> 
          <BBB id = \"b1\"/> 
          <BBB id = \"b2\"/> 
          <BBB name = \"bbb\"/> 
          <BBB/> 
     </AAA>
Copy after login

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=\&#39;bbb\&#39;]
Copy after login

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)=\&#39;bbb\&#39;]
Copy after login

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>
Copy after login

Select an element with 2 sub-elements

     <AAA> 
          <CCC> 
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </CCC> 
          <DDD> 
               <BBB/> 
               <BBB/> 
          </DDD> 
          <EEE> 
               <CCC/> 
               <DDD/> 
          </EEE> 
     </AAA> 
 
//*[count(*)=2]
Copy after login

Select an element with 3 sub-elements


     <AAA> 
          <CCC> 
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </CCC> 
          <DDD> 
               <BBB/> 
               <BBB/> 
          </DDD> 
          <EEE> 
               <CCC/> 
               <DDD/> 
          </EEE> 
     </AAA> 
 
//*[count(*)=3]
Copy after login

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>
Copy after login

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(),\&#39;B\&#39;)]
Copy after login

Select all elements whose names contain \"C\"

<AAA> 
          <BCC> 
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </BCC> 
          <DDB> 
               <BBB/> 
               <BBB/> 
          </DDB> 
          <BEC> 
               <CCC/> 
               <DBD/> 
          </BEC> 
     </AAA> 
 
//*[contains(name(),\&#39;C\&#39;)]
Copy after login

Example 9
Multiple paths can be merged together using the separator |
#//CCC | //BBB

Select all CCC and BBB elements


<AAA> 
          <BCC> 
               <BBB/> 
               <BBB/> 
               <BBB/> 
          </BCC> 
          <DDB> 
               <BBB/> 
               <BBB/> 
          </DDB> 
          <BEC> 
               <CCC/> 
               <DBD/> 
          </BEC> 
     </AAA>
Copy after login

Select all BBB elements and all EEE elements that are child elements of AAA


<AAA> 
          <BBB/> 
          <CCC/> 
          <DDD> 
               <CCC/> 
          </DDD> 
          <EEE/> 
     </AAA> 
 
/AAA/EEE | //BBB
Copy after login

There is no limit to the number of paths that can be merged


<AAA> 
          <BBB/> 
          <CCC/> 
          <DDD> 
               <CCC/> 
          </DDD> 
          <EEE/> 
     </AAA> 
 
/AAA/EEE | //DDD/CCC | /AAA | //BBB
Copy after login

Example 10

The child axis (axis) contains the child elements of the context node, as the default axis , you can ignore it.



/AAA

is equivalent to /child::AAA


   <AAA> 
          <BBB/> 
          <CCC/> 
          <DDD> 
               <CCC/> 
          </DDD> 
          <EEE/> 
     </AAA>
Copy after login

is equivalent to /AAA


     <AAA> 
          <BBB/> 
          <CCC/> 
     </AAA> 
 
/child::AAA
Copy after login

/AAA/BBB

is equivalent to /child::AAA/child::BBB

  <AAA> 
          <BBB/> 
          <CCC/> 
     </AAA>
Copy after login

/child::AAA/child: :BBB
is equivalent to /AAA/BBB

     <AAA> 
          <BBB/> 
          <CCC/> 
     </AAA>
Copy after login
Copy after login

/child::AAA/BBB

Both can be merged


     <AAA> 
          <BBB/> 
          <CCC/> 
     </AAA>
Copy after login
Copy after login

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>
Copy after login

Select 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> 
 
/AAA/BBB/descendant::*
Copy after login

Select all elements with CCC in the ancestor elements

     <AAA> 
          <BBB> 
               <DDD> 
                    <CCC> 
                         <DDD/> 
                         <EEE/> 
                    </CCC> 
               </DDD> 
          </BBB> 
          <CCC> 
               <DDD> 
                    <EEE> 
                         <DDD> 
                              <FFF/> 
                         </DDD> 
                    </EEE> 
               </DDD> 
          </CCC> 
     </AAA> 
 
//CCC/descendant::*
Copy after login

The above is the content of 11 examples of XPath. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!



Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template