4 Replies Latest reply on Feb 16, 2007 8:21 AM by FlightGuy

    E4X question

    Dzihan Level 1
      Hallo all,

      i got following eax statement:

      books..item.(myElement=="myString");

      that shall return als nodes "item" that contain a element "myElement" with the text "myString".

      That works well as long as all my item nodes really contain this myElement.
      In mycase i also got "item" elements that DO NOT contain this myElement, so i get a compiler Error
      "Varible myElement is not defined".

      Anyone know how i can change my statement that only item nodes are regarded that really contain myElement?

      I tried books..item.(myElement && myElement=="myString"); but that doesnt help.

      Thanks in advance,
      Dzihan




        • 1. Re: E4X question
          See if this works for you:

          <books>
          <item />
          <item>
          <myElement>myString</myElement>
          </item>
          <item>
          <myElement>NOTmyString</myElement>
          </item>
          <item>
          <myElement>myString</myElement>
          </item>
          </books>;

          The an E4X expression like this should return what you want:
          books..item.myElement.(text()=="myString").toXMLString()

          -brent
          • 2. Re: E4X question
            Dzihan Level 1
            Hi Brent, thanks for your reply.
            Unfortunately your solution doesn't do the job. Your expression just does the same like mine, it's simply written in another way.
            Once again, the <myElement> node isn't present in all <item> nodes, as soons as the parser reaches a <item> that does not contain it, the error "Varible myElement is not defined".

            At the moment i'm doing this really annoying workaround:

            var allContents:XMLList = myXML..item;
            var neededContents:XML = <books></books>

            for each (var item:XML in allContents) {
            if (item..myElement.length()>0) { neededContents.appendChild(item); }
            }

            I really don't like this loop, i gotta get rid of it.
            • 3. Re: E4X question
              Not sure why it is not working for you. I am am running the following code and not getting any errors (using 2.01). Also note that there is an <item> node that does not have a <myElement> node:

              <mx:Script>
              <![CDATA[
              private var books:XML=<books>
              <item />
              <item>
              <myElement>myString</myElement>
              </item>
              <item>
              <myElement>NOTmyString</myElement>
              </item>
              <item>
              <myElement>myString</myElement>
              </item>
              </books>;

              private function onCreationComplete():void
              {
              // Trace out pieces of myXML3
              trace('~~~~ myXML3 START ~~~~');
              trace('1: ' + books..item.myElement.toXMLString());
              trace('2: ' + books..item.myElement.text());
              trace('3: ' + books..item.myElement.(text()=="myString").toXMLString());
              }
              ]]>
              </mx:Script>

              OUTPUT:
              ~~~~ myXML3 START ~~~~
              1: <myElement>myString</myElement>
              <myElement>NOTmyString</myElement>
              <myElement>myString</myElement>
              2: myStringNOTmyStringmyString
              3: <myElement>myString</myElement>
              <myElement>myString</myElement>