xpath:如何从标记内获取部分数据

时间:2014-04-25 16:06:46

标签: html xpath html-parsing

如果没有获得$ 11.50,我如何获得$ 10.99?

我的代码同时获取,我不想要在标记标记中包含哪些内容:

$price = trim($tmp_xpath->query("//div[@class='ProductPriceRating']/em")->item(0)->nodeValue);

这里是html:

<div class="ProductPriceRating">
    <em><strike class="RetailPriceValue">$11.50</strike> $10.99</em>
    <span class="Rating Rating5"><img src="images/IcoRating5.gif" alt="" style="" /></span>
</div>

1 个答案:

答案 0 :(得分:1)

只需获取text()元素的em

//div[@class='ProductPriceRating']/em/text()

以下是$price的内容:

$price = trim($tmp_xpath->query("//div[@class='ProductPriceRating']/em/text()")->item(0));

或者,正如@adamretter所说,更好地使用normalize-space()代替trim()

$price = $tmp_xpath->query("normalize-space(//div[@class='ProductPriceRating']/em/text())")->item(0);