8 Replies Latest reply on Apr 19, 2007 7:21 AM by MikeD.

    XPATH statement help

    MikeD.
      I'm trying to write a little learning game in Flash, using XML as the data source, but I can't get any XPATH statements to work. I've written tons of XPATH against this structure in JavaScript, and the XPATH statements evaluate properly in XML Spy, but I always get "undefined" when I try to trace out the value of the XPATH statements in Flash 8.

      I'll post a valid (to XML Spy, that is) XPATH statement and some sample code...

      Thanks much for any help!

      Mike

        • 1. Re: XPATH statement help
          Greg Dove Level 4
          I've just started using the XPathAPI Class for the first time in the last hour or so.

          I am unfamiliar with the Javascript implementation. I'm not sure if the Flash class is a full implementation. But I see no mention of an 'array' index in the possible path strings in the documentation. And I just tried it on some of my xml and couldn't make it work. Give this a shot:

          var theXPathString:String = "/interactionData/module/question";
          trace (theXPathString);
          var chosenQuestion= XPathAPI.selectNodeList(this.firstChild, theXPathString)[2];
          var pathToTextValue = "question/theText"
          var theDataString:String = XPathAPI.selectSingleNode(questionNode, pathToTextValue ).firstChild.nodeValue;


          I haven't tested it but I think you might have to do something like that.




          • 2. Re: XPATH statement help
            The Feldkircher Level 2
            Hi

            I think your problem is your making a string reference to a non-existent array element ------- namely - question[2]

            During Parsing place your nodes into an Array then you can reference them directly.

            Hope it helps

            • 3. XPATH statement help
              MikeD. Level 1
              Looks like you are right, when i changed the XPATH statement to

              "/interactionData/module/question/theText";

              it worked fine. But I was trying to get the second question, in proper XPATH 2.0 "/interactionData/module/question[2]/theText" would do so without parsing into an array or anything else.

              Are you saying when I parse the nodes into an array, I can use XPATH against it? Or I'd have to use that horrible child nodes syntax? When I originally wrote the game, I used crazy statements like the one below (attached code) to get stuff out of an XML array. Now I want to take advantage of XPATH to eliminate this horribly inefficient, inflexible code.

              I'm getting a bit frusterated. Not at anyone's responses!!! :-), just at how difficult this seemingly simple thing is. XPATH is so easy and efficient in JavaScript, Java, .NET, XML Spy, etc, etc.

              Thanks for your help, I'll keep experimenting!
              Mike

              • 4. Re: XPATH statement help
                Greg Dove Level 4
                XPathAPI.selectNodeList returns and array of nodes
                childNodes also returns an array of nodes.

                so because you can't reference the specific array index in an xpath string with the current implementation in flash, you have stop at the level of your array, then use actionscript array indexing to return the node that you want.

                Once you have that node, you can use it with another XPathAPI method... if you want... in your case this was selectSingleNode to get to the theText Node (may not be necessary to use xpath when you're that close... but I did it to provide the example. - and because I'm learning how to use it)
                • 5. Re: XPATH statement help
                  MikeD. Level 1
                  Okay, I'm experimenting now, I'll post if I figure out anything useful. Although I admit, I'm not going to waste a lot of time on this, if it looks like it's going to be overly difficult (which it appears at this point it will be) given the limited implementation, I'm just going to port the game to DHTML/Javascript and use a decent XPATH implementation. I have probably 10 or more relatively complex XPATH statements that I want to use, similiar in logic to:

                  get all <theText> nodes whose parent <question> node has a learningWheelUsage attribute set to "yes".

                  This is really simple in every other environment that I work in, and I'm thinking it would just be easier to port the game to something else than try to hack together a solution out of Flash's limited implementation.

                  I'd want to put those into an array of course.. I haven't given up hope yet, I'm going to do some more experimenting, but it is FRUSTERATING!!!!! hahaha..

                  Thanks for the help!
                  Mike

                  • 6. XPATH statement help
                    Greg Dove Level 4
                    well I think that might be possible I haven't tried that level of complexity yet.

                    mytextNodesArr = XPathAPI.selectNodeList(this.firstChild, "/interactionData/module/question[@learningWheelUsage='yes']/theText");

                    (this is untested in terms of my experience, but I base it on the API documentation)

                    Also FYI there is a PDF for the documentation here:

                    http://www.macromedia.com/go/xpathapi
                    • 7. Re: XPATH statement help
                      Greg Dove Level 4
                      Well this test works correctly so you should be able to do what you described with your xml.
                      • 8. Re: XPATH statement help
                        MikeD. Level 1
                        Yea, I'm having some success with that example! So exciting, I think I'll take a long lunch.. haha.. Seriously, thanks a lot, I'm on the right track. It's now just a matter of working with the array, I managed to get all the question nodes that I wanted.