Duplicate Attribute

  • Follow


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. 
0
Reply mittra (27) 7/6/2012 10:38:14 PM

Quoth mittra@juno.com:
> 
> 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

What do you expect the result to be?

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

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.

Ben

0
Reply ben6057 (863) 7/7/2012 12:39:38 AM


On 7 juil, 00:38, mit...@juno.com wrote:
> 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'
        };
==========================================
0
Reply klaus03 (36) 8/4/2012 6:26:36 PM

2 Replies
40 Views

(page loaded in 0.071 seconds)

Similiar Articles:













7/27/2012 6:38:56 PM


Reply: