3 Replies Latest reply on Jan 15, 2009 1:50 PM by Videogy

    Tricky e4x filter question?

    Videogy
      Hi
      I googled this for a day. I didn't find answer. I have an xml node with man tag entries. I want to filter the tag entries based on a condition. Below is a sample of my xml data.


      I want to find all the tag nodes with given string some where in the value. For example, I want to find all the nodes with "book" in them. All the e4x examples on the net talk about filtering based on attribute of current node or child node. But not the value of the node it self. I am actionscript newbee. I am wondering whether there is any way to do this. I know I can use for loop and iterate through each node. I am looking for e4x expression that does this easily.

      var myroot:XML = .... // The root xml data I have at the end
      var tags:XMLList = myroot.tag.(??????????????????????); // What is the filter condition that does it

      Thanks
      Videoguy
        • 1. Re: Tricky e4x filter question?
          flashphpdev
          Here is one example of a way you could do conditional checks using E4X.

          var xmlObj:XML = <langtags>
          <tag name="SEARCH_LABEL">Search</tag>
          <tag name="BOOK_LABEL>Books</tag>
          <tag name="DESCR" >Description of books</tag>
          </langtags>;

          // Give me all "tag" text() value for all nodes that have an attribute "name" == to the condition.
          var condition:XMLList = xmlObj.langtags.tag.(@name == "BOOK_LABEL").text();


          trace(condition); // returns: Books
          • 2. Re: Tricky e4x filter question?
            Videogy Level 1
            Well!
            That is not what I want. I want a condition that returns both the tags that have book in them:

            <tag name="BOOK_LABEL>books</tag>
            <tag name="DESCR" >Description of books</tag>

            Your example used filter condition on "name" attribute. I want a condition that can test the value of each tag node (i.e Books, Description of books).

            Thanks
            • 3. Re: Tricky e4x filter question?
              Videogy Level 1
              I figured a way to do this. Thanks for your example that has a call to text(). This is what I did.

              var tags:XMLList = myroot.tag.(text().indexof("book") >= 0);

              That gave me what I am looking for.