E4X 中的过滤

发布于 2024-09-03 14:01:10 字数 221 浏览 11 评论 0原文

这只是一个简单的问题。我目前正在使用 Mozilla 的 Rhino 开发一个小型网络应用程序。第一步,我需要获取一个网页并过滤它的所有节点。为此,我使用 E4X。我以为我可以这样做:

var pnodes = doc..*(p);

但这会产生错误。怎样做才是正确的?

(顺便说一句:这只是提高性能的一个步骤。代码已经做得很好,只是有点慢。)

This is just a simple question. I'm currently using Mozilla's Rhino to develop a little webapp. As one step, I need to get a webpage and filter all of it's nodes. For doing that, I use E4X. I thought I could do this like that:

var pnodes = doc..*(p);

But that produces an error. How is it done right?

(BTW: this is just a step for increasing performance. The code already does well, it's just a bit slow.)

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

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

发布评论

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

评论(1

素染倾城色 2024-09-10 14:01:10

您应该能够使用以下内容:

doc..*.(name() == "p")

请注意,这里有一个 bug在 Rhino 和 SpiderMonkey 实现中,过滤器表达式 name() == "p" 的范围没有正确限定到当前节点,因此 XML都没有定义了 XMLList 方法。


另一个可行的解决方案是查找文档中的所有 p 节点并将每个节点的父节点累积到一个数组中。

var elements = [];

for each (var p in doc..p) {
    var parent = p.parent();
    if(elements.indexOf(parent) === -1)
        elements.push(parent);
}

You should be able to use the following:

doc..*.(name() == "p")

Note that this there is a bug in the Rhino and SpiderMonkey implementations where the filter expression name() == "p" is not correctly scoped to the current node, so none of the XML or XMLList methods are defined.


Another workable solution is to lookup all p nodes in the document and accumulate the parent of each in an array.

var elements = [];

for each (var p in doc..p) {
    var parent = p.parent();
    if(elements.indexOf(parent) === -1)
        elements.push(parent);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文