Xpath查询无效

时间:2013-09-03 20:25:04

标签: php xpath

这是HTML:

<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">Rachel Zoe Expecting Second&nbsp;Child</a></h2>
</div>
<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="someurl" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">sometitle</a></h2>
</div>

我正在尝试获取锚值。这是我的XPATH:

$finder->query('//div[@class="blogEntry"]//h2//a');

它没有返回任何价值。知道为什么吗?

2 个答案:

答案 0 :(得分:0)

好!!

以下是您要找的内容: -

//div[contains(@class,"blogEntry")]/h2/a/text()

<强>输出

Rachel Zoe Expecting Second Child
sometitle

答案 1 :(得分:0)

您需要在此处使用contains()函数:

$xml = <<<EOF
<div class="blogEntry expecting featured featured-post-today%e2%80%99s-top-story first">
<h2><a href="/2013/09/03/rachel-zoe-pregnant-expecting-second-child/" rel="bookmark" title="Permanent Link to Rachel Zoe Expecting Second&nbsp;Child">Rachel Zoe Expecting Second&nbsp;Child</a></h2>
</div>
EOF;

$doc = new DOMDocument();
$doc->loadHTML($xml);

$selector = new DOMXPath($doc);

foreach($selector->query('//div[contains(@class,"blogEntry")]/h2/a/text()') as $item) {
    echo $item->nodeValue . PHP_EOL;
}

输出:

Rachel Zoe Expecting Second Child
相关问题