views:

269

answers:

2

I am trying to come up with the best xml schema to support tag filtering. And then a method to filter the xml on an arbritary amount of tags. So here is the xml:

var videoXML:XML=
<?xml version="1.0" encoding="UTF-8"?>
    <videos>
        <video> <tags label="dogs,brown,lawns" /> </video>
        <video> <tags label="dogs,cats" /> </video>
        <video> <tags label="cats,lawns" /> </video>
    </videos>

And the way I am filtering now is:

var filteredList:XMLList = videoXML..video.tags.(@label.indexOf("lawns") != -1 && @label.indexOf("dogs") != -1);

which would return only videos that had the tag "lawns" and "dogs", which is all good and fine.

But I want a method that I can pass in as many tags as I want and get the results of that filter.

Something like:

function getFilteredByTags(...tags):XMLList{

}

Any ideas on how to accomplish this?

Thanks!

A: 

I can't come up with a better way but your function could count how many tags there are then run a switch statement on that count and do the right e4x for the number of tags.

invertedSpear
A: 

Untested, but Should Work.™

Change your XML to:

var videoXML:XML=
<?xml version="1.0" encoding="UTF-8"?>
<videos>
    <video>
        <tag label="dogs" />
        <tag label="brown" />
        <tag label="lawns" />
    </video>
    <video>
        <tag label="dogs" />
        <tag label="cats" />
    </video>
    <video>
        <tag label="cats" />
        <tag label="lawns" />
    </video>
</videos>

Function to return videos with multiple tags:

function getFilteredByTags(...tags):XMLList
{
    // Start with a list of all videos
    var foundVideos:XMLList = videoXML.video;

    for each (tag in tags)
    {
        // Filter foundVideos down to those videos that match tag
        foundVideos = foundVideos.(tag.@label == tag);
    }

    return foundVideos;
}
Niko Nyman