in reply to XPathing Up level

XML::XPath - node-to-xpath reverse lookup

Replies are listed 'Best First'.
Re^2: XPathing Up level
by yuvanbala (Initiate) on Dec 03, 2010 at 06:43 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

      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');
      I am not mr_p