Duplicate Attribute

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have an XML that looks like:

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

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


Re: Duplicate Attribute

Quoth mittra@juno.com:
Quoted text here. Click to load it

What do you expect the result to be?

Quoted text here. Click to load it

I don't think so, but you could try the 'recover' option to
XML::LibXML::Parser to see if it will handle that case. You should then
be able to feed the corrected XML to XML::Simple using SAX.


Re: Duplicate Attribute

On 7 juil, 00:38, mit...@juno.com wrote:
Quoted text here. Click to load it

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'

Site Timeline