tags:

views:

55

answers:

2

My XML Looks like this:

<?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]]&gt;&lt;/url&gt;
        <description><![CDATA[Description about the project]]></description>
        <thumb><![CDATA[/upload/images/thumb.jpg]]></thumb>
    </project>
</projects>

But I cannot figure out how to filter projects based on a category id? Does anybody know how to do ? :)

Something like:

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

Just returns all items :(

A: 

Here's a better way without using any custom functions:

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

contains takes a single value to check for in an XML or XMLList object.


You could use an extra function to do the processing:

// 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
Anurag
Awesome thx :) works great but was hoping e4x had some native support for this kind of query.
Chris
You're welcome. There is a better way without using any functions.. updated the answer.
Anurag
A: 

it should be projects.project..(id==3) the double dot skips any nodes, though this is a problem if you have more ids.

Now if I were doing this in actionScript, which is where all of my e4x knowledge comes from I would do this projects.project.containing.(id==3).parent() I'm not sure if JS supports that parent method or maybe it has it's own.

invertedSpear