7 Replies Latest reply on Jul 14, 2011 1:43 PM by insuractive

    XPath question

    nikos101 Level 2

           I know how to do this using for loops but was wondering if it was possible with Xpath.

       

      My xml isl like that:

       

      <ROOT>
          <Batch BatchID="19" >

               
              <Order Swift="AUD"  />

          
              <Order Swift="USD"  />


         </Batch>
          <Batch BatchID="22" ">
              <Order Swift="AUD"  />

           </Batch>

      </ROOT>

       

       

       

      I want to return an array like that:

       

        <Order Swift="AUD"  BatchID="19" />

         <Order Swift="USD"  BatchID="19" />

         <Order Swift="AUD"  BatchID="22"  />

       

      thanks gurus

        • 1. Re: XPath question
          Adam Cameron. Level 5

               I know how to do this using for loops but was wondering if it was possible with Xpath.

           

          Yep.  What have you done by way of finding out how to do it?

           

          For starters, there are some easy to follow tutorials here:

          http://www.zvon.org/comp/r/tut-XPath_1.html

           

          --

          Adam

          • 2. Re: XPath question
            nikos101 Level 2

            thanks,Ive doen research, this is my working code, works but is clunky

             

            <cfscript>
                getData(xmlFeed);
               
                function GetData(xml)
                {
                    allOrders = XmlSearch(xmlFeed, "ROOT/Batch");
               
                    var i = 0;
                    for(i = 1; i <= arraylen(allOrders); i = i + 1)
                    {
                   
                        var children = allOrders[i].XmlChildren;
                        for(var j = 1; j <= arraylen(children); j = j + 1)
                        {
                            structInsert(allOrders[i].XmlChildren[j].XmlAttributes,"BatchID",allOrders[i].XmlAttribut es["BatchID"]);
                        }
                   
                    }
                                    writeDump(allOrders);
                }
               
            </cfscript>

            • 3. Re: XPath question
              nikos101 Level 2

              here is my code ,fixed

               

              <cfhttp method="get" url="http:/52.xml"
                      result="xmlFeed"/>

               

              <cfset xmlFeed = xmlParse(xmlFeed.FileContent)>

               

              <cfscript>
                  getData(xmlFeed);

               

                  function GetData(xml)
                  {
                      allOrders = XmlSearch(xmlFeed, "ROOT/Batch");
                  var final = arrayNew(1);
                      for(i = 1; i <= arraylen(allOrders); i = i + 1)
                      {
                     
                          var children = allOrders[i].XmlChildren;
                          for(var j = 1; j <= arraylen(children); j = j + 1)
                          {
                              structInsert(allOrders[i].XmlChildren[j].XmlAttributes, "BatchID",  allOrders[i].XmlAttributes["BatchID"]);
                                        arrayAppend(final,allOrders[i].XmlChildren[j]);
                          }
                      }
                      writeDump(final );
                  }
                 
              </cfscript>

              • 4. Re: XPath question
                Adam Cameron. Level 5

                You can simplify that a bit. You're after the orders but you're only xpathing as far as the batch. You can path it all the way to the orders: /ROOT/batch/order. That saves some horsing around.

                 

                Also don't forget to always VAR •all• your variables inside functions. Not just some of them.

                 

                --

                Adam

                1 person found this helpful
                • 5. Re: XPath question
                  nikos101 Level 2

                  thanks

                   

                  I know about

                  /ROOT/batch/order

                   

                  but that doesn't get the outer Batch ID in each of the corresponding children, I think my loops are the only way for that?

                  • 6. Re: XPath question
                    Adam Cameron. Level 5

                    Oops... forgot to reply.  Yeah, you're right.  Sorry: didn't notice you needed stuff from the parent nodes too.

                     

                    --

                    Adam

                    • 7. Re: XPath question
                      insuractive Level 3

                      Have you considered applying an XSL transformation instead of trying to parse/write the data in CF?