Spidermonkey 中的 E4X 过滤

发布于 2024-11-15 04:07:33 字数 957 浏览 5 评论 0原文

我在 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 技术交流群。

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

发布评论

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

评论(2

紫罗兰の梦幻 2024-11-22 04:07:33

好的,所以我已经尝试了很多东西,但我仍然无法让原始语法工作,所以我能想到的最好的方法(考虑到这是一个非常好的解决方法)是扩展 XMLList 原型并添加一个filter() 方法:

XMLList.prototype.function::filter = function(callback)
{
    var output  = new XMLList();
    var length  = input.length();
    for(var i = 0; i < length; i++)
    {
        if(callback(input[i], i, input))
        {
            output += input[i];
        }
    }
    return output;
}

因此,使用以下 XML...

var xml = <root>
    <person id="dave">Dave</person>
    <person id="ian">Ian</person>
    <person>John</person>
    <group>
        <person id="jane">Jane</person>
        <person>Sarah</person>
    </group>
</root>

抓取和过滤节点非常直观:

// old
var nodes = xml..person.( hasOwnProperty('@id') );

// new
var nodes = xml..person.filter( function(node){ return node.hasOwnProperty('@id'); } );

<person id="dave">Dave</person>
<person id="ian">Ian</person>
<person id="jane">Jane</person>

如果有人可以纠正我的错误,我会非常高兴,如果没有,我只想知道为什么我的原来的过滤不起作用。

戴夫

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:

XMLList.prototype.function::filter = function(callback)
{
    var output  = new XMLList();
    var length  = input.length();
    for(var i = 0; i < length; i++)
    {
        if(callback(input[i], i, input))
        {
            output += input[i];
        }
    }
    return output;
}

So with the following XML...

var xml = <root>
    <person id="dave">Dave</person>
    <person id="ian">Ian</person>
    <person>John</person>
    <group>
        <person id="jane">Jane</person>
        <person>Sarah</person>
    </group>
</root>

It's pretty intuitive to grab and filter nodes:

// old
var nodes = xml..person.( hasOwnProperty('@id') );

// new
var nodes = xml..person.filter( function(node){ return node.hasOwnProperty('@id'); } );

<person id="dave">Dave</person>
<person id="ian">Ian</person>
<person id="jane">Jane</person>

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

奢华的一滴泪 2024-11-22 04:07:33

只是对此处发布的关于 filter 函数的答案进行了一个小修正:

XMLList.prototype.function::filter = function(callback)
{
    var output  = new XMLList();
    var input = this;
    var length  = input.length();
    for(var i = 0; i < length; i++) {
        //console.println(i + ", " + input[i]);
        if(callback(input[i], i, input))
        {
            //console.println("Yes");
            output += input[i];
        }
    }
    //console.println('output = ' + output);
    return output;
}

原始解决方案是最好的,这正是我正在寻找的!

Just a small correction for the answer posted here, regarding the filter function:

XMLList.prototype.function::filter = function(callback)
{
    var output  = new XMLList();
    var input = this;
    var length  = input.length();
    for(var i = 0; i < length; i++) {
        //console.println(i + ", " + input[i]);
        if(callback(input[i], i, input))
        {
            //console.println("Yes");
            output += input[i];
        }
    }
    //console.println('output = ' + output);
    return output;
}

The original solution is the best and that was exactly what I was looking for!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文