http://www.perlmonks.org?node_id=430867


in reply to XML::Twig generating invalid XHTML output??

This is not quite how I normally use XML::Twig (I avoid the handlers, but that's just me ;-}), but here's a potential solution. Change your constructor to:

my $xml = XML::Twig->new( keep_spaces_in => [ 'pre' ], pretty_print => 'indented', twig_roots => { 'body' => \&insert_form_tags, 'html' => sub {}, }, twig_print_outside_roots => 1, );

That seemed to solve it here.