in reply to multiple for loop to extract required xml nodes

Hello snehit.ar,

From the sample of code that you provide us and the input data files that you provide us I am not getting the output on the second part of your script.

It looks like the events.xml file input is not correct. Update.

Sample of output.

$ perl test.pl $VAR1 = { '1250' => 'dddd', '957' => 'cccc', '1103' => 'bbbb', '2667' => 'aaaa' }; $VAR1 = [];

Looking forward to your update, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: multiple for loop to extract required xml nodes
by snehit.ar (Beadle) on Jul 11, 2017 at 12:04 UTC
    try this my $xpath = "//event/custom_attribute_list/custom_attribute[normalize-space(name)='SSRID']/value"; instead of SLB_SSRID

      Hello again snehit.ar,

      Try to replicate the problem solution based on the code that you give us.

      Check the and update accordingly the code before replying! We can not help you with faulty data.

      #!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); my $appxpath = $xp->findnodes("//application_list/application/"); my %appid = (); foreach my $appnodeset ($appxpath->get_nodelist) { my $id = $xp->find('./@id',$appnodeset)->string_value; my $name = $xp->find('./@name',$appnodeset)->string_value; s/^\s+|\s+$//g for $id,$name; $appid{$id} = $name; } print Dumper \%appid; my $eventxml = 'events.xml'; my $evenxp = XML::XPath->new(filename => $eventxml); my $xpath = "//event/custom_attribute_list/custom_attribute[normalize- +space(name)='SSRID']/value"; my @eventrecords = (); foreach my $node ($evenxp->findnodes($xpath)) { my $ssrid = $node->string_value; $ssrid =~ s/^\s+|\s+$//g ; if ( exists $appid{$ssrid} ){ push @eventrecords, { eventid => $ssrid }; } } print Dumper \@eventrecords; __END__ $ perl test.pl $VAR1 = { '957' => 'cccc', '2667' => 'aaaa', '1250' => 'dddd', '1103' => 'bbbb' }; $VAR1 = [];

      Hope this helps, BR.

      Seeking for Perl wisdom...on the process of learning...not there...yet!
        Hello thanos1983, Sorry for inaccurate data and thanks for your time to help. The needful is done know .