XPath定位深入学习(三)

2021-09-22 11:15:44 浏览数 (2)

一. descendant-or-self

descendant-or-self跟descendant类似,多了节点本身。

  /AAA/BBB/descendant-or-self::*    选择/AAA/BBB本身及所有后代元素

   <AAA>      <BBB>           <DDD>                <CCC>                     <DDD/>                     <EEE/>                </CCC>           </DDD>      </BBB>      <CCC>           <DDD>                <EEE>                     <DDD>                          <FFF/>                     </DDD>                </EEE>           </DDD>      </CCC>

      //CCC/descendant-or-self::*    选择CCC的所有后代元素及本身

   <AAA>

     <BBB>

          <DDD>

               <CCC>

                    <DDD/>

                    <EEE/>

               </CCC>

          </DDD>

     </BBB>

     <CCC>

          <DDD>

               <EEE>

                    <DDD>

                         <FFF/>

                    </DDD>

               </EEE>

          </DDD>

     </CCC>

  </AAA>

二. ancestor-or-self

ancestor-or-self跟ancestor类似,多了本身节点,选取当前节点的所有先辈(父、祖父等)以及当前节点本身。

/AAA/BBB/DDD/CCC/EEE/ancestor-or-self::*   选择/AAA/BBB/DDD/CCC/EEE的先辈及本身

  <AAA>

     <BBB>

          <DDD>

               <CCC>

                    <DDD/>

                    <EEE/>

               </CCC>

          </DDD>

     </BBB>

     <CCC>

          <DDD>

               <EEE>

                    <DDD>

                         <FFF/>

                    </DDD>

               </EEE>

          </DDD>

     </CCC>

   </AAA>

     //FFF/ancestor-or-self::*   选择FFF元素的所有先辈以及FFF本身

   <AAA> 

     <BBB>

          <DDD>

               <CCC>

                    <DDD/>

                    <EEE/>

               </CCC>

          </DDD>

     </BBB>

     <CCC>

          <DDD>

               <EEE>

                    <DDD>

                         <FFF/>

                    </DDD>

               </EEE>

          </DDD>

     </CCC>

三.preceding-sibling

preceding-sibling,选取当前节点之前的所有同级节点,同一个parent下该节点之前的节点,即“哥哥”节点(是同父的哥哥节点)。

  /AAA/XXX/preceding-sibling::*    /AAA/XXX节点的所有之前同级节点

  <AAA>

<EEE/>

     <BBB>

          <CCC/>

          <DDD/>

     </BBB>

     <XXX>

          <DDD>

               <EEE/>

               <DDD/>

               <CCC/>

               <FFF/>

               <FFF>

                    <GGG/>

               </FFF>

          </DDD>

     </XXX>

     <CCC>

          <DDD/>

     </CCC>

   </AAA>

     //CCC/preceding-sibling::*   选取所有CCC节点的同级哥哥节点

  <AAA>

     <BBB>

          <CCC/>

          <DDD/>

     </BBB>

     <XXX>

          <DDD>

               <EEE/>

               <DDD/>

               <CCC/>

               <FFF/>

               <FFF>

                    <GGG/>

               </FFF>

          </DDD>

     </XXX>

     <CCC>

          <DDD/>

     </CCC>

  </AAA>

四.following-sibling

  following-sibling 选取当前节点之后的所有同级节点,跟preceding-sibling一样都是选取同级同父的节点,只不过following是取对应节点之后的节点,preceding-sibling取的是该节点之前的节点。

  /AAA/BBB/following-sibling::*    取/AAA/BBB节点的之后的所有同级节点

<AAA>      <BBB>           <CCC/>           <DDD/>      </BBB>      <XXX>           <DDD>                <EEE/>                <DDD/>                <CCC/>                <FFF/>                <FFF>                     <GGG/>                </FFF>           </DDD>      </XXX>      <CCC>           <DDD/>      </CCC>    </AAA>

     //CCC/following-sibling::*    选取所有CCC元素的之后所有同级节点

<AAA>

     <BBB>

          <CCC/>

          <DDD/>

     </BBB>

     <XXX>

<DDD>

               <EEE/>

               <DDD/>

               <CCC/>

<FFF/>

               <FFF>

                    <GGG/>

</FFF>

          </DDD>

     </XXX>

     <CCC>

          <DDD/>

     </CCC>

   </AAA>

0 人点赞