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
XMLPerl
Last Comment
fabiano petrone
8/22/2022 - Mon
ozo
Same <primary_id> as what? Same as "BRMNTN62M16A944E"
What characters are allowed <first_name> and <last_name>?
fabiano petrone
ASKER
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>
I want to keep only the first <user>...</user> record and delete the second
Thanks a lot,
Fabiano
fabiano petrone
ASKER
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>
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/perluse 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; }}
Hi, Thanks a lot for the reply
is this work possible with the XML::Simple module?
Thanks,
Fabiano
Henry Law
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.
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
What characters are allowed <first_name> and <last_name>?