Search code examples
perlxml-twig

Perl XML::Twig doesn't escape double quotes


I wrote a small script, which parses a XML-file, deletes some redundant elements and writes the rest back to a new XML-file using $xml->print_to_file();.

Everything works fine, except that double quotes inside the tag's text, which have been escaped with " are normal double quotes now. I didn't find a config similar to escape_gt to prevent this behaviour. Is there such a config or an other way to keep double quotes escaped?

My twig config looks like this:

my $xml = XML::Twig->new(
    twig_handlers => {
        label => \&purge_file
    },
    pretty_print => 'indented',
    output_encoding => 'utf-8',
    escape_gt => 1
);

Solution

  • There's no reason to escape quotes in XML text. Double-quotes only need to be escaped in attribute values quoted with double-quotes, and I'm sure you'll find that XML::Twig escapes those.


    ...but it is possible by mucking with XML::Twig's guts:

    XML::Twig::Elt::set_replaced_ents(qq{&<>"});  # "&" needs to be first.
    

    I recommend against this.