格式错误的 HTML 和 XPath 查询
我有一个格式错误的 HTML,无法更改。运行 XPath 查询根本不返回节点:
$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>
<div style="color:#FF0000;"><strong>$ 35.23</strong></div>
</td>
</tr>
</table>
</a>
I can get the a element but I can't get it child (the table)...
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>
<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)...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
由于 libxml 会更改 HTML 以关闭表格之前的 a 元素,因此您必须查询 以下内容-sibling 表,例如
或从 a 元素遍历
请注意,将参数传递给 saveHTML 至少需要 PHP 5.3.6
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.
or traversing from the a element
Note that passing an argument to saveHTML requires at least PHP 5.3.6