Home  >  Article  >  Backend Development  >  XPath 11 instances

XPath 11 instances

黄舟
黄舟Original
2017-02-28 16:40:031501browse

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

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;]

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

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;)]

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>

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

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

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>

is equivalent to /AAA


     <AAA> 
          <BBB/> 
          <CCC/> 
     </AAA> 
 
/child::AAA

/AAA/BBB

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>

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::*

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::*

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)!



Statement:
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