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.
I ended up going with usort()
Manipulation of the DOM tree became increasingly complicated, so a callback function was necessary.