e4x过滤多个孩子?

时间:2010-05-19 08:36:53

标签: javascript e4x

我的XML看起来像这样:

<?xml version="1.0" encoding="utf-8" ?>
<projects>
    <project 
        id="1"
        thumb="media/images/thumb.jpg"
        >
        <categories>
            <id>1</id>
            <id>2</id>
        </categories>
        <director>Director name</director>
        <name><![CDATA[IPhone commercial]]></name>
        <url><![CDATA[http://www.iphone.com]]></url>
        <description><![CDATA[Description about the project]]></description>
        <thumb><![CDATA[/upload/images/thumb.jpg]]></thumb>
    </project>
</projects>

但我无法弄清楚如何根据类别ID过滤项目?有人知道该怎么办吗? :)

类似的东西:

projects.project.(categories.(id == 3))

只返回所有项目:(

2 个答案:

答案 0 :(得分:0)

如果不使用任何自定义函数,这是一种更好的方法:

projects.project.(categories.id.contains(1))

contains在XML或XMLList对象中检查单个值。


您可以使用额外的功能进行处理:

// check if any of the <id> nodes matches any of the given values
function containing(nodes, values) {
    for each(var id in nodes) {
        if(values.indexOf(parseInt(id)) !== -1) return true;
    }
    return false;
}

projects.project.(containing(categories.id, [1])); // matches the first project
projects.project.(containing(categories.id, [46])); // matches nothing

答案 1 :(得分:0)

它应该是projects.project..(id==3)双点跳过任何节点,但如果你有更多的id,这是一个问题。

现在,如果我在动作脚本中执行此操作,这是我所有e4x知识的来源,我会这样做projects.project.containing.(id==3).parent()我不确定JS是否支持该父方法,或者它是否拥有它。< / p>

相关问题