Reputation: 85
I'm selectively fixing some elements and attributes. Unfortunately, our input files contain both single- and double-quoted attribute values. Also, some attribute values contain quotes (within a value).
Using XML::Twig, I cannot see out how to preserve whatever quotes exist around attribute values.
Here's sample code:
use strict;
use XML::Twig;
my $file=qq(<file>
<label1 attr='This "works"!' />
<label2 attr="This 'works'!" />
</file>
);
my $fixes=0; # count fixes
my $twig = XML::Twig->new( twig_handlers => {
'[@attr]' => sub {fix_att(@_,\$fixes);} },
# ...
keep_atts_order => 1,
keep_spaces => 1,
keep_encoding => 1, );
#$twig->set_quote('single');
$twig->parse($file);
print $twig->sprint();
sub fix_att {
my ($t,$elt,$fixes) =@_;
# ...
}
The above code returns invalid XML for label1:
<label1 attr="This "works"!" />
If I add:
$twig->set_quote('single');
Then we would see invalid XML for label2:
<label2 attr='This 'works'!' />
Is there an option to preserve existing quotes? Or is there a better approach for selectively fixing twigs?
Upvotes: 4
Views: 447
Reputation: 16171
Is there any specific reason for you to use keep_encoding
? Without it the quote is properly encoded.
keep_encoding
is used to preserve the original encoding of the file, but there are other ways to do this. It was used mostly in the pre-5.8 era, when encodings didn't work as smoothly as they do now.
Upvotes: 2