XPath查询以获取另一个节点的后代节点

时间:2011-08-24 16:08:28

标签: xml xpath domdocument

我有以下HTML代码

<html>
<body>
  ~~ optional text and variably-nested elements ~~
  <div class="a">
    ~~ optional text and variably-nested elements ~~
    <div class="b">example</div>
    ~~ optional text and variably-nested elements ~~
  </div>
  ~~ optional text and variably-nested elements ~~
  <div class="c">
    ~~ optional text and variably-nested elements ~~
    <div class="b">example</div>
    ~~ optional text and variably-nested elements ~~
  </div>
  ~~ optional text and variably-nested elements ~~
</body>
</html>

我想检索<div class="b">的{​​{1}} DOMNode。

我用过:

<div class="c">

但它产生了错误的结果。什么是正确的XPath查询使用?

由于

2 个答案:

答案 0 :(得分:2)

尝试

"//*[@class='c']//*[@class='b']"

http://codepad.org/EzBAWDPg

答案 1 :(得分:0)

使用此XPath://div[@class = 'c']/div[@class = 'b']

它会选择div[@class = 'b']的孩子div[@class = 'c']