开发者

Malformed HTML and XPath query

I have a malformed HTML that I can't change. Running a XPath Query doesn't return the nodes at all:

$el = $xpath->query("//a[@class='product']/table"); // can get a tag with "//a[@class='product']"
print_r($el->length); // 0

Malformed HTML:

<a class="product" href="#">
    <table width="385" cellspacing="0" cellpadding="5" style="border:1px; border-bottom-color:#E2E2E2; border-bottom-style:solid;">
        <tr>
            <td width="55">
                <img src="http://foobar.com:8080/img/1212.jpg" height="50" width="50">
            </td>
        <td width="195">Cod.27731<br>Product Name</td>
            <td width="60" align="center"><a href="?pageContent=items&price=fab&prodcod=27731">Details</a></td>
            <td width="80" nowrap>
                <开发者_如何学Python;div style="color:#FF0000;"><strong>$ 35.23</strong></div>
        </td>
        </tr>
    </table>
</a>

I can get the a element but I can't get its child (the table)...


Since libxml will change the HTML to close the a element before the table, you have to query for the following-sibling table instead, e.g.

$dom = new DOMDocument;
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);
$el = $xpath->query("//a[@class='product']/following-sibling::table");
echo $dom->saveHtml($el->item(0));

or traversing from the a element

$dom = new DOMDocument;
$dom->loadHtml($html);
$xpath = new DOMXpath($dom);
$table = $xpath->query("//a[@class='product']")->item(0)->nextSibling;
echo $dom->saveHtml($table);

Note that passing an argument to saveHTML requires at least PHP 5.3.6

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜