php xpath 表解析问题 [英] php xpath table parsing question

查看:21
本文介绍了php xpath 表解析问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 php xpath 解析的表中嵌套了多个表.

I have a several tables nested within a table that I am parsing using php xpath.

我使用了一系列 xpath,因为我将代码分解为跨多个方法调用的概念单元,并且这种结构在没有嵌套表的其他场景中也能完美运行.

I'm using a series of xpaths because I'm breaking up the code into conceptual units across several methods calls, and this structure has been working perfectly in other scenarios without nested tables.

代码如下:

// create a host DOM document
$dom = new DOMDocument();

// load the html string into the dom
$dom->loadHTML($html_string);

// make an xpath object out of the dom
$xpath = new DOMXpath($dom);

// run query to extract the rows from the master table
$context_nodes = $xpath->query('//table[@id="id1"]/tr[position()>1]');

// parse data from the individual tables nested in each master table row
foreach($context_nodes as $context_node){
    $interesting_nodes[] = $xpath->query('table[2]/tr[td[2]]', $context_node);
}

生成的 $interesting_nodes 数组包含空的 DOMNodeLists.

The resulting $interesting_nodes array contains empty DOMNodeLists.

$context_nodes DOMNodeList 包含有效数据.每个 $context_node 的 html 内容如下所示:

The $context_nodes DOMNodeList contains valid data. The html content of each $context_node looks like this:

<td>
    <table></table>
    <table>
        <tr>
            <td></td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
</td>

我尝试了以下简化的 $intesting_nodes 查询来匹配任何表:

I tried the following simplified $intesting_nodes query to match any table:

$intesting_nodes[] = $xpath->query('table', $context_node);

但这仍然会产生相同的空 DOMNodeLists.

But that still produces the same empty DOMNodeLists.

现在是有趣的部分

当我像这样尝试 $interesting_nodes 查询时:

When I try an $interesting_nodes query like so:

$interesting_nodes[] = $xpath->query('*[2]/*[*[2]]', $context_node);

然后一切正常完美;但是如果我用相应的table"、tr"或td"标签替换any*",那么查询将再次中断.

Then everything works perfectly; but if I replace any "*" with the corresponding "table", "tr", or "td" tags, then the query breaks once again.

有没有其他人有过这种行为和 php 中的相关 xpath 查询的经验?

Does anyone else have experience with this behavior and relative xpath queries in php?

我非常希望能够使用更精确的查询,并且希望能够保持查询的相对性,而不是使其绝对化.

I would very much like to be able to use a more exact query, and would prefer to be able to keep the query relative like it is rather than making it absolute.

推荐答案

我想通了.:)

如果主表标签不存在,php xpath 实现不知道如何处理表内部节点(即:tr、td).

The php xpath implementation does not know what to do with table internal nodes (ie: tr, td) if the master table tags are not present.

我的外部 td 标签导致 xpath 查询出现意外结果.

My outer td tags were causing unexpected results from the xpath query.

将 $context_nodes 查询修改为:

Modified the $context_nodes query to:

$context_nodes = $xpath->query('//table[@id="id1"]/tr[position()>1]/td');

我们很好.

这篇关于php xpath 表解析问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆