按节点深度对 DOMXPath 查询结果排序

发布于 2024-10-12 09:56:00 字数 2173 浏览 5 评论 0原文

给出这个示例文档片段:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:zuq="http://localhost/~/zuqml">
<head></head>
<body>
    <zuq:data name="a" />
    <div>
        <zuq:data name="b" />
    </div>
    <div>
        <zuq:data name="c">
            <zuq:format type="trim">
                <zuq:param name="length">300</zuq:param>
                <zuq:param name="append">
                    <span>
                        <zuq:data name="d" />
                    </span>
                </zuq:param>
            </zuq:format>
        </zuq:data>
    </div>
    <div>
        <zuq:data name="e">
            <zuq:format type="trim">
                <zuq:param name="length">300</zuq:param>
                <zuq:param name="append">
                    <span>
                        <zuq:data name="f" />
                    </span>
                </zuq:param>
            </zuq:format>
        </zuq:data>
    </div>
</body>
</html>

我试图抓取 节点进行操作,但按一定顺序:最深的第一个

我目前已经做到这一点:

$nodeList = $xpath->query('.//zuq:data[not(ancestor::zuq:region)]');
foreach($nodeList as $node){
    $nodeOrder[] = Array($xpath->evaluate('count(ancestor::*)', $node) => $node->getAttribute('name'));
}

print_r($nodeOrder);

当然会吐出这样的内容:

Array
(
[0] => Array
    (
        [2] => a
    )
[1] => Array
    (
        [3] => b
    )
[2] => Array
    (
        [3] => c
    )
[3] => Array
    (
        [7] => d
    )
[4] => Array
    (
        [3] => e
    )
[5] => Array
    (
        [7] => f
    )
)

现在我正在考虑仅使用 array_multi_sort()usort() 来订购DOMNode 对象进行操作。然而,在我看来,有人可能知道一种更有效的方法来解决这个问题。

有什么想法吗?


注意:对于其他不必要的实现细节,我的 XPath 查询字符串所做的超出了此处相关的范围。我刚刚将这个例子从我目前所在的位置拼凑起来。

Given this example document snippet:

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:zuq="http://localhost/~/zuqml">
<head></head>
<body>
    <zuq:data name="a" />
    <div>
        <zuq:data name="b" />
    </div>
    <div>
        <zuq:data name="c">
            <zuq:format type="trim">
                <zuq:param name="length">300</zuq:param>
                <zuq:param name="append">
                    <span>
                        <zuq:data name="d" />
                    </span>
                </zuq:param>
            </zuq:format>
        </zuq:data>
    </div>
    <div>
        <zuq:data name="e">
            <zuq:format type="trim">
                <zuq:param name="length">300</zuq:param>
                <zuq:param name="append">
                    <span>
                        <zuq:data name="f" />
                    </span>
                </zuq:param>
            </zuq:format>
        </zuq:data>
    </div>
</body>
</html>

I'm trying to grab the <zuq:data /> nodes to manipulate, but in a certain order: deepest first.

I've currently gotten up to this point:

$nodeList = $xpath->query('.//zuq:data[not(ancestor::zuq:region)]');
foreach($nodeList as $node){
    $nodeOrder[] = Array($xpath->evaluate('count(ancestor::*)', $node) => $node->getAttribute('name'));
}

print_r($nodeOrder);

Which of course spits out this:

Array
(
[0] => Array
    (
        [2] => a
    )
[1] => Array
    (
        [3] => b
    )
[2] => Array
    (
        [3] => c
    )
[3] => Array
    (
        [7] => d
    )
[4] => Array
    (
        [3] => e
    )
[5] => Array
    (
        [7] => f
    )
)

Now I'm considering just using array_multi_sort() or usort() to order the DOMNode objects for manipulation if I were to go forward with my current 'solution'. However, it seems to me that someone might know of a more efficient way to go about this.

Any thoughts?


Note: My XPath query string is doing more than what is relevant here, for other unnecessary implementation details. I just pieced this example together from where I'm at currently.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

望喜 2024-10-19 09:56:00

我最终选择了 usort() DOM 树的操作变得越来越复杂,因此需要一个回调函数。

I ended up going with usort() Manipulation of the DOM tree became increasingly complicated, so a callback function was necessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文