Filtering in E4X
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 inc开发者_StackOverflow中文版reasing performance. The code already does well, it's just a bit slow.)
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);
}
精彩评论