本文介绍了如何根据 XPath 中嵌套兄弟的值选择元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在包含大量 DIV 的文档中,我想选择 asset-body
元素内的文本,但前提是 bookmark
链接内的 asset-name entry-title 在 href
值中包含文本 democrat
.
因此,在下面的示例中,包含两个条目"(entry-1
和 entry-2
),我只需要 Jackpot!
因为该条目"中 bookmark
链接的值包含子字符串 democrat
:
http://blahblah.com/politics-democrat
是否可以使用 XPath 执行此操作?
条目 1:(失败:该条目中的 bookmark
链接缺少子字符串 democrat
)
<div class="asset-header"><h2 class="asset-name entry-title"><a rel="bookmark" href="http://blahblah.com/paper-scissors">剪纸</a>
<div class="asset-content entry-content"><div class="asset-body"><p>纸和剪刀</p>
ENTRY 2: (PASS: bookmark
该条目中的链接包含子字符串 democrat
)
<div class="asset-header"><h2 class="asset-name entry-title"><a rel="bookmark" href="http://blahblah.com/politics-democrat">佩洛西问答</a>
<div class="asset-content entry-content"><div class="asset-body"><p>头奖!</p>
解决方案
//div[contains(格/小时2[contains(concat(' ',@class,' '),' 资产名称 ')和contains(concat(' ',@class,' '),' entry-title ')]/a[@rel='书签']/@href,'民主党人')]/格/格[contains(concat(' ',@class,' '),' 资产主体 ')]//文本()
In a document containing a large number of DIVs, I want to select the text inside the asset-body
elements, but only if the bookmark
link inside the asset-name entry-title
of that "entry" contains the text democrat
in the href
value.
So, in the example below, containing two "entries" (entry-1
and entry-2
), I only want the text that says Jackpot!
because the value of the bookmark
link in that "entry" contains the substring democrat
:
http://blahblah.com/politics-democrat
Is it possible to do this using XPath?
ENTRY 1: (FAIL: bookmark
link in that entry lacks the substring democrat
)
<div id="entry-1" class="item-asset asset hentry">
<div class="asset-header">
<h2 class="asset-name entry-title">
<a rel="bookmark" href="http://blahblah.com/paper-scissors">Paper Scissors</a>
</h2>
</div>
<div class="asset-content entry-content">
<div class="asset-body">
<p>Paper and scissors</p>
</div>
</div>
</div>
ENTRY 2: (PASS: bookmark
link in that entry contains the substring democrat
)
<div id="entry-2" class="item-asset asset hentry">
<div class="asset-header">
<h2 class="asset-name entry-title">
<a rel="bookmark" href="http://blahblah.com/politics-democrat">Pelosi Q&A</a>
</h2>
</div>
<div class="asset-content entry-content">
<div class="asset-body">
<p>Jackpot!</p>
</div>
</div>
</div>
解决方案
//div[contains(
div/h2[
contains(concat(' ',@class,' '),' asset-name ')
and
contains(concat(' ',@class,' '),' entry-title ')
]/a[@rel='bookmark']/@href
,'democrat')
]/div/div[
contains(concat(' ',@class,' '),' asset-body ')
]//text()
这篇关于如何根据 XPath 中嵌套兄弟的值选择元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
08-21 09:05