【发布时间】:2015-07-10 17:15:51
【问题描述】:
我有一些代码块,需要从中获取数据并尝试不同版本的 xpath 命令,但没有成功。
<div>
<div class="some_class">
<a title="id" href="some_href">
<nobr>1<br>
</a>
</div>
<div class="some_other_class">
<a title="name" href="some_href">
<nobr>John<br>
</a>
</div>
</div>
<div>
<div class="some_class">
<a title="id" href="some_href">
<nobr>2<br>
</a>
</div>
<div class="some_other_class">
<a title="name" href="some_href">
<nobr>John<br>
</a>
</div>
</div>
// and many blocks like this
所以,这个 div 块是相同的,只是它们的子元素的内容不同。我需要 xpath 查询来获取 John 的 href,<a title="id"> 等于 1。
我尝试过这样的事情:
//div[./div/nobr='1' AND ./div/nobr='John']
只获取包含我需要的数据的 div,然后不难获取 John 的 href。
另外,我设法通过以下方式获得了 John 的 href:
//a[./nobr='John'][@title='name']/@href
但这样它不依赖于来自 <a title="id"...> 元素的值,但它必须依赖它。
有什么建议吗?
【问题讨论】:
-
如果你想得到“John's href”,为什么不创建一个John's href 与其他
href属性不同的例子,这样你就可以清楚地表达你想要什么? -
另外,请注意 XPath 仅在格式良好的标记上定义,而您的示例不是:有多个根元素和多个未闭合元素。
-
这与 href 无关,我已经明确表示我需要在 John 的“超级元素”内的 href,即
John 是的子元素a> 标签具有我需要的 href 属性。未闭合元素 是一个错字。谢谢你慷慨的小评论:) -
对不起,如果你真的说清楚了,我们现在已经为你回答了你的问题。祝你好运。