snehit.ar has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I have 2 XML file and Want to compare the "id" in (application.xml) with "ssrid" in (event.xml) and print only matching id and retrieve "name" of matching id from appication.xml as output ?

---application.xml

<application_list> <application id="2667" external-id="EAR-AA-2667" name="aaaa"></applica +tion> <application id="1103" external-id="EAR-AA-1103" name="bbbb"></applica +tion> <application id="957" external-id="EAR-AA-957" name="cccc"></applicati +on> <application id="1250" external-id="EAR-AA-1250" name="dddd"></applica +tion> </application_list>
-----Events.xml
<event_list> <event> <name> Incident</name> <value> INC0004532345324</value> <name> SSRID</name> <value> 957</value> </event> </event_list>

---Perl code

#!/usr/bin/env perl use strict; use warnings; use XML::XPath; use Data::Dumper; use List::Compare; my @records; my @eventrecords; my $eventxml = 'events.xml'; my $evenxp = XML::XPath->new(filename => $eventxml); my $evennodeset = $evenxp->findnodes('//event'); foreach my $evennode ($evennodeset->get_nodelist) { my $evenssrid = $evenxp->find("./custom_attribute_list/custom_ +attribute[normalize-space(name)='SLB_SSRID']/value", $evennode); s/^\s+|\s+$//g for $evenssrid; push @eventrecords, {eventid => $evenssrid}; } print Dumper \@eventrecords; my $xml = 'ApplicationList.xml'; my $xp = XML::XPath->new(filename => $xml); #my $nodeset = $xp->findnodes('//application_list'); for ($xp->findnodes('/application_list/application/@id') ) { my $appid = $_->string_value; $appid =~ s/^\s+|\s+$//g; push @records, {appid => $appid}; } print Dumper \@records; if ( scalar List::Compare->new(\@records, \@eventrecords)->get_int +ersection ) { print "The arrays are the same"; #print Dumper @eventrecords; }

Thanks in advance !

Replies are listed 'Best First'.
Re: Compare 2 XML files
by choroba (Cardinal) on Jul 10, 2017 at 11:49 UTC
    I'd hash the ids from the file that's shorter, then iterate over the larger file and check the presence of the id in the hash. For example, if events.xml is shorter, you can use the following in xsh, a wrapper around XML::LibXML :
    open 'events.xml' ; $ssrid := hash normalize-space(value[normalize-space(preceding::name[1])='SSRI +D']) /event_list/event ; open 'application.xml' ; for /application_list/application { if xsh:lookup('ssrid', @id) echo @id ; }
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: Compare 2 XML files
by Corion (Patriarch) on Jul 10, 2017 at 07:28 UTC

    The easiest approach is to extract all SSRIDs (and the corresponding data) and all the ids and the corresponding data and then find the intersection of the two arrays.

      For the given example can you please suggest the needfull... I am new to perl so from given forum not able to get any thing ...
      Also i am not able to retrieve the application id from the node ..

        The first thing would be to actually extract your $ssrids from the XML and print them.

        Then, extract the system IDs from the other XML and print those.

        After you have convinced yourself that you can extract the correct values, apply the hint of how to find matching elements between two lists.

        Maybe you need to take a step back and consider whether/how you can actually extract the data you need.

Re: Compare 2 XML files
by karlgoethebier (Abbot) on Jul 10, 2017 at 07:40 UTC

    See also Set::Scalar: $i = $s->intersection($t); or $i = $s * $t;

    Regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

    perl -MCrypt::CBC -E 'say Crypt::CBC->new(-key=>'kgb',-cipher=>"Blowfish")->decrypt_hex($ENV{KARL});'Help