Spidermonkey 中的 E4X 过滤
我在 SpiderMonkey 中使用 E4X,大多数语言看起来相当可靠,但我无法让过滤工作:
var xml = <root>
<person id="dave">Dave</person>
<person id="ian">Ian</person>
<person>John</person>
</root>
trace( xml.*.(name() == 'person') );
trace( xml.*.(attribute('@id')) );
预期:
<person id="dave">Dave</person>
<person id="ian">Ian</person>
<person>John</person>
<person id="dave">Dave</person>
<person id="ian">Ian</person>
结果:
ReferenceError: name is not defined
ReferenceError: attribute is not defined
我什至无法让 hasOwnProperty() 工作:
xml.*.(trace( hasOwnProperty('@id') ));
false
false
false
具体来说,我在Flash中使用JSFL,它使用SpiderMonkey引擎。
从我到目前为止对 E4X 的基本了解来看,这是相当意外的/有问题的,对吧?因为我可以让这些表达式在 ActionScript / FlashPlayer 中正常工作!
谢谢, 戴夫
I'm using E4X in SpiderMonkey, and the majority of the language seems pretty solid, but I can't get filtering to work:
var xml = <root>
<person id="dave">Dave</person>
<person id="ian">Ian</person>
<person>John</person>
</root>
trace( xml.*.(name() == 'person') );
trace( xml.*.(attribute('@id')) );
Expected:
<person id="dave">Dave</person>
<person id="ian">Ian</person>
<person>John</person>
<person id="dave">Dave</person>
<person id="ian">Ian</person>
Results:
ReferenceError: name is not defined
ReferenceError: attribute is not defined
I can't even get hasOwnProperty() to work either:
xml.*.(trace( hasOwnProperty('@id') ));
false
false
false
Specifically, I'm using JSFL in Flash, which uses the SpiderMonkey engine.
From my basic knowledge of E4X so far, this is pretty unexpected / buggy, right? As I can get these expressions to work just fine in ActionScript / FlashPlayer!
Thanks,
Dave
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,所以我已经尝试了很多东西,但我仍然无法让原始语法工作,所以我能想到的最好的方法(考虑到这是一个非常好的解决方法)是扩展 XMLList 原型并添加一个filter() 方法:
因此,使用以下 XML...
抓取和过滤节点非常直观:
如果有人可以纠正我的错误,我会非常高兴,如果没有,我只想知道为什么我的原来的过滤不起作用。
戴夫
OK, so I've tried a bunch of things, and I still can't get the original syntax to work, so the best I can come with (it's a pretty good workaround, considering) is to extend the XMLList prototype and add a filter() method:
So with the following XML...
It's pretty intuitive to grab and filter nodes:
If anyone can correct me on this, I'd be super-glad, if not, I'd just like to know why my original filtering just didn't work.
Dave
只是对此处发布的关于
filter
函数的答案进行了一个小修正:原始解决方案是最好的,这正是我正在寻找的!
Just a small correction for the answer posted here, regarding the
filter
function:The original solution is the best and that was exactly what I was looking for!