• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

XPath question

Enthusiast ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

     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

TOPICS
Advanced techniques

Views

677

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

     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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

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.XmlChildren;
            for(var j = 1; j <= arraylen(children); j = j + 1)
            {
                structInsert(allOrders.XmlChildren.XmlAttributes,"BatchID",allOrders.XmlAttributes["BatchID"]);
            }
       
        }
                        writeDump(allOrders);
    }
   
</cfscript>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

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.XmlChildren;
            for(var j = 1; j <= arraylen(children); j = j + 1)
            {
                structInsert(allOrders.XmlChildren.XmlAttributes, "BatchID",  allOrders.XmlAttributes["BatchID"]);
                          arrayAppend(final,allOrders.XmlChildren);
            }
        }
        writeDump(final );
    }
   
</cfscript>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Enthusiast ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
May 28, 2011 May 28, 2011

Copy link to clipboard

Copied

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

--

Adam

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Advocate ,
Jul 14, 2011 Jul 14, 2011

Copy link to clipboard

Copied

LATEST

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation