XPath 查询中的 XPath 查询


XPath query in an XPath query

如何在 XPatch 查询中启动第二个和第三个 XPatch 查询?
例如

代码板 = http://codepad.viper-7.com/ZhMNGw

网页代码

<div class="entries">                               
    <h3 class="headline" style="position: relative; cursor: pointer;">
        <div>
            <a class="selink" href="/tste/?sd=28726585">  
                <span class="date"> 10:15 </span> 
                <span class="titel">THE TITLE<span class="subtitel">some subtitle</span>
                </span>
            </a>
        </div>                              
    </h3>
</div>  
<div class="entries">                               
    <h3 class="headline" style="position: relative; cursor: pointer;">
        <div>
            <a class="selink" href="/tste/?sd=287265995">  
                <span class="date"> 10:16 </span> 
                <span class="titel">THE TITLE 2<span class="subtitel">some subtitle</span>
                </span>
            </a>
        </div>                              
    </h3>
</div>  

.PHP

libxml_use_internal_errors(true);
$doc = new DOMDocument;
$doc->preserveWhiteSpace = false;
$doc->strictErrorChecking = false;
$doc->recover = true;
$doc->loadHTMLFile('http://domain.com/startpage.php');
$xpath = new DOMXPath($doc);
$query = "//div[@class='entries']"; // <- QUERY ONE
$entries = $xpath->query($query);
$list = array();
$count = 0;
foreach ($entries as $key => $value)
{        
    $list[$count] = array();
    // get the link <- QUERY TWO
    $list[$count]['url'] =  $xpath->query("//a[@class='selink']");
    // get the title but NOT the subtitle <- QUERY THREE
    $list[$count]['title'] = $xpath->query("//span[@class='titel']");

    $count++;
}

print_r($list);

$xpath->query($expr) 在循环中的每次调用上对整个文档执行,因为您没有传递应相对计算 XPath 查询的文档节点。

使用多态方法 DOMNodeList query(字符串$expr,DOMNode $node),您可以执行相对于给定$node的子查询。仅当使用相对 XPath $expr(不带前导/)时,此方法才会生成所需的结果。要从每个 DOMNode/TextNode 检索字符串,请按如下方式使用查询:

$list[$count]['url'] = $xpath->query("h3/div/a[@class='selink']/@href", $value)->item(0)->value;
$list[$count]['title'] = $xpath->query("h3/div/a/span[@class='titel']/text()", $value)->item(0)->wholeText;

我在这里编辑了你的CodePad代码。

问候麦克斯