2010-07-06 178 views
3

我想解析一個html文件。php xpath:查詢結果內的查詢

這個想法是用titledesc類獲取跨度,並在每個具有屬性class ='thebest'的div中獲取它們的信息。

這裏是我的代碼:

<?php 

$example=<<<KFIR 
<html> 
<head> 
<title>test</title> 
</head> 
<body> 
<div class="a">moshe1 
<div class="aa">haim</div> 
</div> 
<div class="a">moshe2</div> 
<div class="b">moshe3</div> 

<div class="thebest"> 
<span class="title">title1</span> 
<span class="desc">desc1</span> 
</div> 
<div class="thebest"> 
span class="title">title2</span> 
<span class="desc">desc2</span> 
</div> 

</body> 
</html> 
KFIR; 


$doc = new DOMDocument(); 
@$doc->loadHTML($example); 
$xpath = new DOMXPath($doc); 
$expression="//div[@class='thebest']"; 
$arts = $xpath->query($expression); 

foreach ($arts as $art) { 
    $arts2=$xpath->query("//span[@class='title']",$art); 
    echo $arts2->item(0)->nodeValue; 
    $arts2=$xpath->query("//span[@class='desc']",$art); 
    echo $arts2->item(0)->nodeValue; 
} 
echo "done"; 

預期的結果是:

title1desc1title2desc2done 

是我收到的結果是:

title1desc1title1desc1done 

回答

10

使相關查詢...以點開始(例如".//…")。

foreach ($arts as $art) { 
    // Note: single slash (direct child) 
    $titles = $xpath->query("./span[@class='title']", $art); 
    if ($titles->length > 0) { 
     $title = $titles->item(0)->nodeValue; 
     echo $title; 
    } 

    $descs = $xpath->query("./span[@class='desc']", $art); 
    if ($descs->length > 0) { 
     $desc = $descs->item(0)->nodeValue; 
     echo $desc; 
    } 
} 
1

而不是做第二次查詢嘗試textContent

foreach ($arts as $art) { 
    echo $art->textContent; 
} 

textContent返回此節點及其後代的文本內容。

作爲替代方案,所述的XPath更改爲

$expression="//div[@class='thebest']/span[@class='title' or @class='desc']"; 
$arts = $xpath->query($expression); 

foreach ($arts as $art) { 
    echo $art->nodeValue; 
} 

這將與THEBEST具有類標題或內容描述的一類取的div的跨度的兒童。