use strict; use warnings; use XML::Twig; my $xml = q( name1
address1
contact1
name2
address2
contact2
); my %handlers = ( 'name[string() =~ /name2/]' => sub { my ($twig, $cnt) = @_; $cnt->parent->delete;} ); my $twig= new XML::Twig( PrettyPrint => 'indented', twig_handlers => \%handlers); $twig->parse($xml); print $twig->sprint;