Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

XPathing Up level

by mr_p (Scribe)
on Dec 02, 2010 at 22:51 UTC ( #875032=perlquestion: print w/ replies, xml ) Need Help??
mr_p has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks,

I am in need of your help again. All the help is apprecicated.

I am tring to traverse an xml get values from it. The program is suppose to parse 'Country' and 'WID'attribute value. The output should look like the below:

USA|1 UK|1 CANADA|1 MEXICO|2 INDIA|2 CHINA|2

but the problem is I am not able to pullout WID value so my out put is incorrect. What am I doing wrong? Please help

#!/usr/bin/perl my $xml_str1 = '<List> <Events WID="1"> <Event Confirmed="Y"> <Country>USA</Country> </Event> <Event Confirmed="Y"> <Country>UK</Country> </Event> <Event Confirmed="Y"> <Country>CANADA</Country> </Event> </Events> <Events WID="2"> <Event Confirmed="Y"> <Country>MEXICO</Country> </Event> <Event Confirmed="Y"> <Country>INDIA</Country> </Event> <Event Confirmed="Y"> <Country>CHINA</Country> </Event> </Events> </List>'; parse_xml($xml_str1); sub parse_xml { use XML::LibXML; no warnings; my $Country = (); my @List = (); my $parser = XML::LibXML->new(); my $doc = $parser->parse_string($_[0]); foreach my $ca_isins ($doc->findnodes('/List//Events/Event')) { my ($titleCountry) = $ca_isins->findnodes('./Country'); $Country = $titleCountry->to_literal . "\n"; my ($widValue) = $ca_isins->findvalue('../../@Wid'); $Country = $titleCountry->to_literal . "|" . $widValue . "\n"; push (@List, $Country); } print "@List"; }

Comment on XPathing Up level
Select or Download Code
Re: XPathing Up level
by Anonymous Monk on Dec 03, 2010 at 02:35 UTC
      Hi,

      try this code for your search

      my $xml_str = '<List> <Events WID="1"> <Event Confirmed="Y"> <Country>USA</Country> </Event> <Event Confirmed="Y"> <Country>UK</Country> </Event> <Event Confirmed="Y"> <Country>CANADA</Country> </Event> </Events> <Events WID="2"> <Event Confirmed="Y"> <Country>MEXICO</Country> </Event> <Event Confirmed="Y"> <Country>INDIA</Country> </Event> <Event Confirmed="Y"> <Country>CHINA</Country> </Event> </Events> </List>'; parse_xml($xml_str); sub parse_xml { use XML::LibXML; no warnings; my $Country = (); my @List = (); my $parser = XML::LibXML->new(); my $doc = $parser->parse_string($_[0]); foreach my $ca_isins ($doc->findnodes('/List/Events')) { my ($id) = $ca_isins->findnodes('@WID'); $widValue=$id->to_literal; my (@titleCountry) = $ca_isins->findnodes('./Event//Country'); + foreach (@titleCountry){ push (@List, ($_->to_literal, '|', $widValue, "\n")); } } print "@List"; }
      Thanks and Regards,
      yuvanbala
        I am not mr_p

        Thanks so much. That worked perfectly.

        One more question?

        why is it ./Event//Country and not ./Event/Country, with single /?

Re: XPathing Up level
by choroba (Abbot) on Dec 03, 2010 at 15:06 UTC
    Using XML::XSH2, you can just write
    open 875032.xml ; for /List/Events/Event/Country echo :s (.) '|' ../../@WID ;
Re: XPathing Up level
by ikegami (Pope) on Dec 03, 2010 at 16:50 UTC

    '../../@Wid' should be '../@Wid' as Events is the direct parent of Event.

    That said, I would use the same nested loop approach as yuvanbala.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://875032]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-10-24 08:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (131 votes), past polls