5 Replies Latest reply on Jul 28, 2008 11:50 AM by ntsiii

    E4X filtering

    Guus Eikens
      I'm working on a FAQ application, in which I want to dynamically filter a large set Frequently Asked Questions. I want the filtering action only to be fired on the question-labels, but not on the categories. Categories have to be always visible, questions depending on the search term. Can this be done using a smart E4X expression?

      The XML script to for loading the faqs looks like:
      [Bindable]
      private var faqData:XML =
      <root>
      <faq>
      <c label="Category 1">
      <q label="Question 1">
      Answer 1.
      </q>
      <q label="Question 2">
      Answer 2.
      </q>
      <q label="Question 3">
      Answer 3.
      </q>
      </c>
      <c label="Category 2">
      <q label="Question 4">
      Answer 4.
      </q>
      <q label="Question 5">
      Answer 5.
      </q>
      </c>
      </faq>
      </root>;

      The E4X expression (including "parentheses operators" for smart filtering) to get only the questions containing the search term is:
      var xmlFiltered:XMLList = faqData..q.(hasOwnProperty('@label') && @label.search(strSearch) > -1 );
      ... but I need their categories AS WELL !

        • 1. E4X filtering
          VarioPegged Level 2
          Will the filtered data always only be from one category? In other words, are you only filtering the full label "Question 1" in strSearch? Then you can use the parent() method to get the category the question belongs to.

          var strSearch:String = "Question 1";
          var xmlFiltered:XMLList = faqData..q.(attribute("label") == strSearch);
          trace(xmlFiltered); //returns Answer 1.
          var category:XMLList = xmlFiltered.parent().attribute("label");
          trace(category); //returns Category 1

          If you're instead expecting multiple returns for the filtered data by searching on any number of characters, you could do this:

          var xmlFiltered:XMLList = faqData..q.(attribute("label").search(strSearch) > -1);

          xmlFiltered may now contain data from multiple categories, so parent() will return "undefined" ... you could loop through xmlFiltered to get the respective parents (categories) of the questions.

          var arrLength:int = xmlFiltered.length();

          for (var a:int = 0; a < arrLength; a++)
          {
          trace(xmlFiltered[a].parent().attribute("label"));
          }

          TS
          • 2. Re: E4X filtering
            ntsiii Level 3
            Use parent() to get the Category node. Also, see the example below for a cleaner way to handle missing attributes in a search:
            var xmlFiltered:XMLList = faqData..q.(attribute("label").search(strSearch) > -1 ).parent();

            @label will error if there are any nodes without that attribute but attribute("label") will not.

            Tracy
            • 3. Re: E4X filtering
              Guus Eikens Level 1
              Wow! Looks like magic, like the first time I became aware of the power of regular expressions. Thanks for the tips, I think I might get somewhere. Am I getting it right that "faqData..q" is a pure E4X expression? And that an expression between parentheses operators ("("and ")") is something from the ActionScript language? But then: how do you call a combined expression like:
              faqData..q.(attribute("label").search(strSearch) > -1 ).parent();
              ???

              And: can anybody recommend me a book or website on advanced XML processing in Flex?
              • 4. Re: E4X filtering
                Level 7

                "Guus Eikens" <webforumsuser@macromedia.com> wrote in message
                news:g6fttm$rn4$1@forums.macromedia.com...
                > Wow! Looks like magic, like the first time I became aware of the power of
                > regular expressions. Thanks for the tips, I think I might get somewhere.
                > Am I
                > getting it right that "faqData..q" is a pure E4X expression? And that an
                > expression between parentheses operators ("("and ")") is something from
                > the
                > ActionScript language? But then: how do you call a combined expression
                > like:
                > faqData..q.(attribute("label").search(strSearch) > -1 ).parent();
                > ???
                >
                > And: can anybody recommend me a book or website on advanced XML processing
                > in
                > Flex?
                >

                http://dispatchevent.org/roger/as3-e4x-rundown/

                HTH;

                Amy


                • 5. Re: E4X filtering
                  ntsiii Level 3
                  The expression in parens must simply evaluate to a Boolean. You can even call another function, if you have complex logic to apply.

                  It is really all ActionScript, so the e4x expression can access all AS functions or user components in scope.

                  Tracy