简体   繁体   中英

Ordering DOMXPath query results by node-depth

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.

我最终使用usort()操作,因为DOM树的操作变得越来越复杂,因此必须使用回调函数。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM