2012-06-18 66 views
0

比方说,我有一个HTML的结构是这样 - > (注:在我的代码有没有身份证,我只是把他们这里,所以它更容易解释)的XPath获取特定子

<body> 
    <table id=a> 
     <tr> 
     <td> 
      <table id=b> 
      <tr> 
       <td> 
       </td> 
      </tr> 
      </table> 
     </td> 
     </tr> 
    </table> 
    <table id=c> 
     <tr> 
     <td> 
     </td> 
     </tr> 
    </table> 
    </body> 

使用XPath ,//body/table[1]给我的ID为“B”的内部表,但我真正想要的是它的第一个孩子(“C”)。沿着第$("body >table").eq(1)行的东西,但我使用C#。我如何使用XPath来做到这一点?

Tnx!

编辑: 这是不是一种选择,我选择[2]元素,因为这只是我正在冻胀问题的简化的解释...

安德烈

+0

有看看这个问题 http://stackoverflow.com/questions/8713177/get-first-specific-node-wi th-random-optional-subnodes –

+0

是不是第一个元素0? //体/表[0] – Vale

回答

1

元素选择器从索引1开始(而不是从0开始)。因此,您将不得不使用xpath查询//body/table[2]

下面是屏幕截图的解释。

对于基于0的索引节点将为空;

enter image description here

但由于该指数从1开始,为的indeces 1和2个的这些节点将被退回

enter image description here

enter image description here

还是不相信?让我们来检查内部表格元素。

首先让我们以0基于索引检查(同样也将是空)

enter image description here

但是,当指数为1,将返回正确的节点

enter image description here

0

尝试

//body/table[not(ancestor::td)]