按节点深度对 DOMXPath 查询结果排序
给出这个示例文档片段:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终选择了
usort()
DOM 树的操作变得越来越复杂,因此需要一个回调函数。I ended up going with
usort()
Manipulation of the DOM tree became increasingly complicated, so a callback function was necessary.