开发者

mulitple attribute xml filter as3 e4x

开发者 https://www.devze.com 2022-12-28 07:56 出处:网络
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:

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" /> <开发者_Python百科/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!


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.


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;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消