in reply to Re: XPathing Up level
in thread XPathing Up level

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

Replies are listed 'Best First'.
Re^3: XPathing Up level
by mr_p (Scribe) on Dec 04, 2010 at 05:52 UTC

    Thanks so much. That worked perfectly.

    One more question?

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

      A single slash would be sufficient. For that matter, the leading "./" is completely useless.

      my @titleCountry = $ca_isins->findnodes('Event/Country');
Re^3: XPathing Up level
by Anonymous Monk on Dec 03, 2010 at 07:24 UTC
    I am not mr_p