使用Xpath检索元素

时间:2013-03-06 16:42:01

标签: xpath

HTML部分:

<div class="abc">
    <div style="text-align:left; itemscopr itemtype="xyz">
        <h1 itemtype="mno"> I want this text </h1>
    </div>
</div>

我正在使用

$text = $xpath->query('//div[class="abc"]/div/h1]

但我没有任何价值。请帮助我,因为我是新手。

1 个答案:

答案 0 :(得分:1)

你应该试试

//div[@class="abc"]/div/h1

区别在于@之前的class符号,因为the attribute axis is accessed this way。省略@符号时,它会查找节点名称(标记名称)。

这会返回整个h1节点(或者更确切地说,是一个包含所有匹配的h1节点的节点集)。

如果您只想要元素中的文本,请尝试使用evaluate函数:

$text = $xpath->evaluate("//div[@class='abc']/div/h1/text()")