html AgilityPack XPath C#.net 4

时间:2013-03-18 16:12:02

标签: xml xpath html-parsing html-agility-pack

我必须定义xpath来访问第二个span文本,以解析加载到AgilityPack文档的以下名称 - 值html结构(见下文)。我想使用xpath和C#进行以下查询:

var valueSpan = doc.SelectNodes("//div[@class='container']/div[@class='value2' and ???]/span[2]");

问题:我无法为“第二个匹配的div with class ==”value2“编写xpath选择器。是否可以使用xpath选择第二个”value2“div,或者它应该通过索引”容器“节点的子节点来完成(“value2”的实际兄弟指数可能会有所不同)?

<div class="container">
  <div class="value1">
    <span class="name">Title 1</span>
    <span class="value">some value</span>
  </div>
  <div class="value2">
    <span class="name">Title 2.1</span>
    <span class="value">some value</span>
  </div>
  <div class="value2">
    <span class="name">Title 2.2</span>
    <span class="value">some value</span>
  </div>
  <div class="value3">
    <span class="name">Title 3</span>
    <span class="value">some value</span>
  </div>
</div>

1 个答案:

答案 0 :(得分:0)

使用:

//div[@class='container']/div[@class='value2'][2]