我试图通过使用带有以下HTML(相同结构)和代码的PHP的DOM元素,从div中获取class = 'review-text'的文本。
然而,这似乎不起作用
HTML
$html = '
<div class="page-wrapper">
<section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
<article class="review clearfix">
<div class="review-content">
<div class="review-text" itemprop="reviewBody">
Outstanding ...
</div>
</div>
</article>
</section>
</div>
';
PHP代码
$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class and contains(concat(' ', normalize-space(@class), ' '), ' $classname ')]");
if ($results->length > 0) {
echo $review = $results->item(0)->nodeValue;
}
在此Blog
提供了按类选择元素的XPATH语法我在StackOverflow,在线教程中尝试过很多例子,但似乎都没有。我错过了什么吗?
答案 0 :(得分:25)
以下XPath查询可以执行您想要的操作。只需使用以下内容替换为$ xpath-&gt;查询提供的参数:
//div[@class="review-text"]
编辑: 为了便于开发,您可以在http://www.xpathtester.com/test在线测试自己的XPath查询。
EDIT2: 测试了这段代码;它运作得很好。
<?php
$html = '
<div class="page-wrapper">
<section class="page single-review" itemtype="http://schema.org/Review" itemscope="" itemprop="review">
<article class="review clearfix">
<div class="review-content">
<div class="review-text" itemprop="reviewBody">
Outstanding ...
</div>
</div>
</article>
</section>
</div>
';
$classname = 'review-text';
$dom = new DOMDocument;
$dom->loadHTML($html);
$xpath = new DOMXPath($dom);
$results = $xpath->query("//*[@class='" . $classname . "']");
if ($results->length > 0) {
echo $review = $results->item(0)->nodeValue;
}
?>
答案 1 :(得分:4)
扩展Frak Houweling回答,也可以使用DomXpath在特定DomNode内进行搜索。这可以通过将contextNode
作为第二个参数传递给DomXpath->query
方法来实现:
$dom = new DOMDocument;
$dom->loadHTML ($html);
$xpath = new DOMXPath ($dom);
foreach ($xpath->query ("//section[@class='page single-review']") as $section)
{
// search for sub nodes inside each element
foreach ($xpath->query (".//div[@class='review-text']", $section) as $review)
{
echo $review->nodeValue;
}
}
请注意,在搜索内部节点时,您需要在表达式的开头添加一个点.
来使用相对路径:
"//div[@class='review-text']" // absolute path, search starts from the root element
".//div[@class='review-text']" // relative path, search starts from the provided contextNode