首页 > 代码库 > XPath 元素查找方法

XPath 元素查找方法

 

/AAA
选择根元素AAA

      <AAA>
           <BBB/>
           <CCC/>
           <BBB/>
           <BBB/>
           <DDD>
                <BBB/>
           </DDD>
           <CCC/>
      </AAA>

 

 

/AAA/CCC
选择AAA的所有CCC子元素

      <AAA>
           <BBB/>
           <CCC/>
           <BBB/>
           <BBB/>
           <DDD>
                <BBB/>
           </DDD>
           <CCC/>
      </AAA>

 

 

/AAA/DDD/BBB
选择AAA的子元素DDD的所有子元素

      <AAA>
           <BBB/>
           <CCC/>
           <BBB/>
           <BBB/>
           <DDD>
                <BBB/>
           </DDD>
           <CCC/>
      </AAA>

 

实例 2

如果路径以双斜线 // 开头, 则表示选择文档中所有满足双斜线//之后规则的元素(无论层级关系)

 

//BBB
选择所有BBB元素

      <AAA>
           <BBB/>
           <CCC/>
           <BBB/>
           <DDD>
                <BBB/>
           </DDD>
           <CCC>
                <DDD>
                     <BBB/>
                     <BBB/>
                </DDD>
           </CCC>
      </AAA>

 

 

//DDD/BBB
选择所有父元素是DDD的BBB元素

      <AAA>
           <BBB/>
           <CCC/>
           <BBB/>
           <DDD>
                <BBB/>
           </DDD>
           <CCC>
                <DDD>
                     <BBB/>
                     <BBB/>
                </DDD>
           </CCC>
      </AAA>

 

实例 3

星号 * 表示选择所有由星号之前的路径所定位的元素

 

选择所有路径依附于/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
选择所有的有3个祖先元素的BBB元素

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

 

 

//*
选择所有元素

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

 

实例 4

方块号里的表达式可以进一步的指定元素, 其中数字表示元素在选择集里的位置, 而last()函数则表示选择集中的最后一个元素.

 

/AAA/BBB[1]
选择AAA的第一个BBB子元素

      <AAA>
           <BBB/>
           <BBB/>
           <BBB/>
           <BBB/>
      </AAA>

 

 

/AAA/BBB[last()]
选择AAA的最后一个BBB子元素

      <AAA>
           <BBB/>
           <BBB/>
           <BBB/>
           <BBB/>
      </AAA>

 

实例 5

属性通过前缀 @ 来指定

 

//@id
选择所有的id属性

      <AAA>
           <BBB id = "b1"/>
           <BBB id = "b2"/>
           <BBB name = "bbb"/>
           <BBB/>
      </AAA>

 

 

//BBB[@id]
选择有id属性的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB id = "b2"/>
           <BBB name = "bbb"/>
           <BBB/>
      </AAA>

 

 

//BBB[@name]
选择有name属性的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB id = "b2"/>
           <BBB name = "bbb"/>
           <BBB/>
      </AAA>

 

 

//BBB[@*]
选择有任意属性的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB id = "b2"/>
           <BBB name = "bbb"/>
           <BBB/>
      </AAA>

 

 

//BBB[not(@*)]
选择没有属性的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB id = "b2"/>
           <BBB name = "bbb"/>
           <BBB/>
      </AAA>

 

实例 6

属性的值可以被用来作为选择的准则, normalize-space函数删除了前部和尾部的空格, 并且把连续的空格串替换为一个单一的空格

 

 

 

//BBB[@id=‘b1‘]
选择含有属性id且其值为‘b1‘的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB name = " bbb "/>
           <BBB name = "bbb"/>
      </AAA>

 

 

//BBB[@name=‘bbb‘]
选择含有属性name且其值为‘bbb‘的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB name = " bbb "/>
           <BBB name = "bbb"/>
      </AAA>

 

 

//BBB[normalize-space(@name)=‘bbb‘]
选择含有属性name且其值(在用normalize-space函数去掉前后空格后)为‘bbb‘的BBB元素

      <AAA>
           <BBB id = "b1"/>
           <BBB name = " bbb "/>
           <BBB name = "bbb"/>
      </AAA>

XPath 元素查找方法