Perl processing of an XML file

Hello,
an xml file contains user records like:

<user>
<account_type>EXTERNAL</account_type>
<primary_id>BRMNTN62M16A944E</primary_id>
<first_name>John</first_name>
<last_name>Doe</last_name>

...other fields...

<user_identifier>
<id_type desc="Additional">02</id_type>
<value>john.doe@mymail.com</value>
</user_identifier>

...other fields...

</user>

Open in new window

it happens that some records have the SAME <primary_id> but the <value> of the <user_identifier> of <type> 02 (see above) have a value different than "<first_name>.<last_name>@mymail.com"
is it possible individuating & suppressing all and only these <user> records?
Thanks a lot,
Fabiano
fabiano petroneAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ozoCommented:
Same <primary_id> as what? Same as "BRMNTN62M16A944E"
What characters are allowed <first_name> and <last_name>?
fabiano petroneAuthor Commented:
Hi, ozo
yes: same as "BRMNTN62M16A944E".
i.e. there're 2 records with
1) same <primary_id>BRMNTN62M16A944E</primary_id>

2) same first & second, say:
<first_name>John</first_name>
<last_name>Doe</last_name>

3) but different email, say:
<user_identifier>
<id_type desc="Additional">02</id_type>
<value>john.doe@mymail.com</value>
</user_identifier>

for the first record and

<user_identifier>
<id_type desc="Additional">02</id_type>
<value>something.else@mymail.com</value>
</user_identifier>

I want to keep only the first <user>...</user> record and delete the second
Thanks a lot,
Fabiano
fabiano petroneAuthor Commented:
Hi, ozo
to make the things more clearer (I hope) I've attached a sample file with 2 <user> records.
They have the same value for the fields:
<primary_id>BRMNTN62M16A944E</primary_id>
<first_name>john</first_name>
<last_name>doe</last_name>

but different value in the user_identifier field.
for the first record:
<user_identifier>
<id_type desc="Additional">02</id_type>
<value>john.doe@uniud.it</value>
</user_identifier>

and for the second one:

<user_identifier>
<id_type desc="Additional">02</id_type>
<value>someOther.thing@uniud.it</value>
</user_identifier>

I want to keep only the first <user> record and delete the second.
Thanks,
Fabiano
sample.xml
OWASP Proactive Controls

Learn the most important control and control categories that every architect and developer should include in their projects.

Henry LawCommented:
You need to parse your XML with a Perl module which gives you access to all its elements, in a hierarchy that you can use to find whatever you want and delete it.   I generally use XML::LibXML for this.  Here's a little bit of code that may help you get going; XML::LibXML and its related modules (XML::LibXML::Node and XML::LibXML::Element are the ones you'll use most) will do everything you want.

#!/usr/bin/perl
use strict;
use warnings;
use 5.010;
use XML::LibXML;

my $dom = XML::LibXML->load_xml(location => 'sample.xml');

my @primary_ids = $dom->findnodes('/users/user/primary_id');

my $previous_id = '';
foreach my $id_elt ( @primary_ids ){
    my $this_id = $id_elt->to_literal();
    if ( $this_id eq $previous_id ){
	my @user_identifiers = $id_elt->parentNode()->findnodes('./user_identifiers/user_identifier');
	print "User identifiers for $this_id: \n";
	print "-----------------\n$_\n" for @user_identifiers;
        print "Here you do other things as required\n";
    }
    else {
	$previous_id = $this_id;
    }
}

Open in new window

fabiano petroneAuthor Commented:
Hi, Thanks a lot for the reply
is this work possible with the XML::Simple module?
Thanks,
Fabiano
Henry LawCommented:
You could try, but i personally wouldn't recommend it.  XML::Simple makes assumptions about the way that the XML data is represented by variables--arrays, hashes and so forth--which may not be the best way of doing it from the programming point of view.  There are parameters that you can pass which change that, but the way the arrays and hashes are created has a tendency to change with the structure of the data, so that your program may fail at some later stage because perfectly valid XML is structured in a new way.  The CPAN entry for XML::Simple even says this: "The use of this module in new code is discouraged. Other modules are available which provide more straightforward and consistent interfaces"

If you know XML::Simple then this page may be of interest.
fabiano petroneAuthor Commented:
Hi,
Thanks for the reply.
I use W7 with activeperl 5.24.0 and on the ppm I can find the XML::Simple and XML::Parser modules, but sadly not the XML::LibXML module
Henry LawCommented:
That's frustrating.  I used to use ActivePerl (before I switched to Linux) and thought they were pretty good.  People have managed to install XML::LibXML (see here) but it doesn't sound straightforward.

There are lots of other XML parsing packages that will probably do; it's just that I don't have experience of it. I do know that doing what you want to do with XML::Simple is not a good idea!

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
fabiano petroneAuthor Commented:
Hi, Henry
Thanks: I'll follow your suggestions
Thanks,
Fabiano
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.