my $doc = XML::LibXML->new->load_xml( string => $before ); my @tests = $doc->findnodes('/suite/test[@name="Import Test Suite"]'); my $suite = $doc->getDocumentElement; $suite->removeChildNodes; $suite->addChild($_) for @tests; print $doc->serialize(1); # or $doc->toFile("new.xml")