Duplicate Attribute

M

mittra

I have an XML that looks like:

<abc attr1="val1" attr2="val2" attr1="val3">
</abc>

Yes, this is illegal per XML rule since we have two attr1 and when I parse it through XML::Simple, I get this error message:

duplicate attribute at line 14, column 291, byte 1936 at /usr/lib/perl5/XML/Parser.pm line 187

However, I am wondering if there is a way to tell XML::Simple to ignore this error.

Thanks.
 
K

Klaus

I have an XML that looks like:

<abc attr1="val1" attr2="val2" attr1="val3">
</abc>

Yes, this is illegal per XML rule since we have two attr1 and when I parse it through XML::Simple, I get this error message:

duplicate attribute at line 14, column 291, byte 1936 at /usr/lib/perl5/XML/Parser.pm line 187

However, I am wondering if there is a way to tell XML::Simple to ignore this error.

Maybe XML::Reader (version 0.44) can help out:

==========================================
use strict;
use warnings;

use XML::Reader 0.44 qw(XML::parsepp slurp_xml);
use XML::Simple;
use Data::Dumper;

my $xml_with_dup =
q{<abc attr1='val1' attr2='val2' attr1='val3'></abc>};

my $xml_without_dup = slurp_xml(\$xml_with_dup,
{ dupatt => '|' },
{ root => '/', branch => '*' })->[0][0];

my $ref = XMLin($xml_without_dup);

print "With Dup = $xml_with_dup\n";
print "Without Dup = $xml_without_dup\n";
print Dumper($ref), "\n";
==========================================

This is the output:
==========================================
With Dup = <abc attr1='val1' attr2='val2' attr1='val3'></abc>
Without Dup = <abc attr1='val1|val3' attr2='val2'></abc>
$VAR1 = {
'attr2' => 'val2',
'attr1' => 'val1|val3'
};
==========================================
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top