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 !

In reply to Compare 2 XML files by snehit.ar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.