The mangling I see is removing the DOCTYPE, or removing the PUBLIC identifier from the DOCTYPE. I think you can turn off loading of the DTD. This may make XML::Twig not change the prolog. You can also access the doctype with doctype() and set it with set_doctype().