我是xpath的新手,并且已经坚持了太长时间。是否可以从下面的示例中获取upc值0305064523219?
我正在尝试//div[@class='prdct3']/strong[4]
,但当然只包括“UPC:&#39;在强标签之间。我需要转到<br />
标记。有没有办法让xpath获取元素之后的元素?
<div class="prdct3">
<p><strong>Description:</strong></p>
<p><a href="/catalog/?JOJ4026UP=jojoblister-card-2-
colors-asstd">Digital Watch on Blister Card 2 Colors Asstd.</a></p>
<strong>Item Size:</strong> 6.75" x 3.5" x .75"<br />
<strong>Case:</strong> 144 <strong>Inner:</strong> 24<br />
<strong>UPC:</strong> 0305064523219<br />
<strong>WT:</strong> 10 <strong>CF:</strong> 1.82<br />
<strong>Sub-Category:</strong> AC06<br />
<strong>Origin:</strong> China<br /><div class="floatRight"><a
class='iframe' href="/_forms/?JOJ4032P"><img
src="/_images/btn_questions.jpg" alt="" /></a></div>
</div>
答案 0 :(得分:2)
以下似乎有效:
function xpath(selector, root) {
return document.evaluate(selector, root || document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
var node = xpath('//div[@class="prdct3"]/strong[text()="UPC:"]/following-sibling::text()[1]');
console.log(node.textContent);
&#13;
<div class="prdct3">
<p><strong>Description:</strong></p>
<p><a href="/catalog/?JOJ4026UP=jojoblister-card-2-
colors-asstd">Digital Watch on Blister Card 2 Colors Asstd.</a></p>
<strong>Item Size:</strong> 6.75" x 3.5" x .75"<br />
<strong>Case:</strong> 144 <strong>Inner:</strong> 24<br />
<strong>UPC:</strong> 0305064523219<br />
<strong>WT:</strong> 10 <strong>CF:</strong> 1.82<br />
<strong>Sub-Category:</strong> AC06<br />
<strong>Origin:</strong> China<br /><div class="floatRight"><a
class='iframe' href="/_forms/?JOJ4032P"><img
src="/_images/btn_questions.jpg" alt="" /></a></div>
</div>
&#13;
我们找到文本为strong
的{{1}}标记,然后获取以下第一个兄弟文本节点。
UPC:
是axis。