Posted on 2008-06-18 02:35:09-07 by krayzee
Attribute parsing bug in XML::Stream::Parser
The "attribution" function in XML/Stream/Parser.pm is broken. In particular the 2nd if that tries to work out whether to use ' or " for the attribute value is supposed to be the inversion of the previous if but it's not. And as a result attributes that come in as
version='1.0' xmlns="whatever"
result in it only parsing version="whatever" because the second if conludes that the " come before the '.
Solutions:
1) Make the second if an elsif instead. This works
2) Get rid of the second if altogether and make that code block just an else of the first if. This works. And is cleaner
3) Rewrite the second if to be the same as the first but replace id1 with id2 & vice versa throughout. DO NOT change the equalities to inequalities.
4) Replace the entire function with this (the function already uses regex's and in a bad way: while ($str =~ s/\s//) {} is just horrible and should be $str =~ s/\s//g; instead but that doesn't matter in the following version):
sub attribution { my $self = shift; my $str = shift; $str = "" unless defined($str); my %attribs; while ($str =~ /(\S+)\s*=\s*(["'])(.*?)\2\s*(.*)/) $attribs{$1} = $self->entityCheck($3); $str = $4; } return %attribs; }
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.