use warnings; use strict; use XML::Twig; my $twig = new XML::Twig( twig_handlers => { item => \&item, }, ); $twig->parsefile('in.xml'); $twig->set_pretty_print('indented'); $twig->print_to_file('out.xml'); sub item { my ( $twig, $item ) = @_; for my $c ($item->children()) { $c->cut($item) unless $c->tag() eq 'id' or $c->tag() eq 'name' ; } }