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"; }
In reply to XPathing Up level by mr_p
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |