The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.


XML::DoubleEncodedEntities - unbreak XML with doubly-encoded entities


Occasionally, XML files escape into the wild with their entities encoded twice so instead of this:

    <chocolate>Green &amp; Blacks</chocolate>

you get:

    &lt;chocolate&gt;Green &amp;amp; Blacks&lt;/chocolate&gt;

A real-world example of this problem can be seen in this failing test for a module which queries an online XML datasource:

(search for the text 'Arcturus' in that page).

This module tries to fix that.


    use XML::DoubleEncodedEntities;
    my $xmlfile = XML::DoubleEncodedEntities::decode($xmlfile);



This function is not exported, but can be if you wish. It takes one scalar parameter and returns a corresponding scalar, decoded if necessary.

The parameter is assumed to be a string. If its first non-whitespace characters are &lt;, or if it contains the sequence &amp;amp; the string is assumed to be a doubly-encoded XML document, in which case the following entities, if present, are decoded: &amp; &lt; &gt; &quot; &apos;

No other parameters are decoded. After all, if the input document has been *doubly* encoded then something like æ, which should be the entity &aelig; will be represented by the character sequence &amp;aelig;. Once the &amp; has been corrected by this module, you'll be able to decode the resulting &aelig; in the normal way.


I welcome feedback about my code, including constructive criticism. Bug reports should be made using or by email, and should include the smallest possible chunk of code, along with any necessary data, which demonstrates the bug. Ideally, this will be in the form of a file which I can drop in to the module's test suite. Ideally such files will work in perl 5.004.

If you are feeling particularly generous you can encourage me in my open source endeavours by buying me something from my wishlist:


Encode::DoubleEncodedUTF8, which does the same job for broken UTF-8.

Test::DoubleEncodedEntities, which is HTMLish.


David Cantrell <>


Copyright 2007 David Cantrell

This module is free-as-in-speech software, and may be used, distributed, and modified under the same terms as Perl itself.


This module is also free-as-in-mason software.