1 Reply Latest reply on Oct 10, 2009 6:31 PM by byuhobbes85

    Where have the xpath queries gone?

    byuhobbes85

      I have some XML data that I am working with. See the example below.

      var data:XML = XML(<itemBody>
                           <container>
                             <text>Read the following passage and select the appropriate choices.</text>
                           </container>
                           <container>
                             <text>
                               Rudolf the red-nosed reindeer had a very shiny
                               <inlineChoiceInteraction shuffle="true">
                                 <inlineChoice identifier="01.choice.01">pose</inlineChoice>
                                 <inlineChoice identifier="01.choice.02">nose</inlineChoice>
                                 <inlineChoice identifier="01.choice.03">hose</inlineChoice>
                               </inlineChoiceInteraction>,
                               And if you ever saw it you would even say it
                               <inlineChoiceInteraction shuffle="true">
                                 <inlineChoice identifier="02.choice.01">knows</inlineChoice>
                                 <inlineChoice identifier="02.choice.02">blows</inlineChoice>
                                 <inlineChoice identifier="02.choice.03">glows</inlineChoice>
                               </inlineChoiceInteraction>.
                             </text>
                           </container>
                         </itemBody>);
      
      var interactions:XMLList = data.container.text.inlineChoiceInteraction;
      // This also works
      // var interactions:XMLList = data.*.*.inlineChoiceInteraction;
      Alert.show("Number of interactions: "+ interactions.length());
      

       

      Using e4x, I have no problem navigating the XML structure and pulling out the elements I need -- in this case the inlineChoiceInteraction elements. However, the format of may data may not be predictable -- in other words, inlineChoiceInteraction may be nested to an arbitrary number of levels. XPath is the natural solution for this type of problem -- an XPath query like //inlineChoiceInteraction will grab all nodes of that type regardless of their location in the data structure.

       

      It appears that older versions of ActionScript had XPath support, but not the current version. I've found several homegrown implementations, but none of them were very clear to me. Is there a way to do what I want to do with e4x or is there some XPath support I don't know about or do I need to start writing some recursive functions every time I want to do something like this?

       

      Thanks.

        • 1. Re: Where have the xpath queries gone?
          byuhobbes85 Level 1

          So, I had seen a package called Xpath-AS3. However, I hadn't seen its official documentation. Once I found the official documentation, the solution was pretty simple.  Here are the steps.

          • Download the SWC and put it in your project's lib folder.
          • Add three import statements (see below).
          • Create an XPathQuery object (see below).
          • Call the XPathQuery object's exec function and hand it your XML.

           

          // Make sure these are at top
          import memorphic.parser.*;
          import memorphic.utils.*;
          import memorphic.xpath.*;
          
          // Now, in your function...
          var data:XML = XML(<itemBody>
                               <container>
                                 <text>Read the following passage and select the appropriate choices.</text>
                               </container>
                               <container>
                                 <text>
                                   Rudolf the red-nosed reindeer had a very shiny
                                   <inlineChoiceInteraction shuffle="true">
                                     <inlineChoice identifier="01.choice.01">pose</inlineChoice>
                                     <inlineChoice identifier="01.choice.02">nose</inlineChoice>
                                     <inlineChoice identifier="01.choice.03">hose</inlineChoice>
                                   </inlineChoiceInteraction>,
                                   And if you ever saw it you would even say it
                                   <inlineChoiceInteraction shuffle="true">
                                     <inlineChoice identifier="02.choice.01">knows</inlineChoice>
                                     <inlineChoice identifier="02.choice.02">blows</inlineChoice>
                                     <inlineChoice identifier="02.choice.03">glows</inlineChoice>
                                   </inlineChoiceInteraction>.
                                 </text>
                               </container>
                             </itemBody>);
          
          var interactionQuery:XPathQuery = new XPathQuery("//inlineChoiceInteraction");
          var interactions:XMLList = interactionQuery.exec(data);
          Alert.show("Number of interactions: "+ interactions.length());
          

           

          The documentation is pretty clear on how to handle namespaces and anything else you would need to consider with Xpath queries.