Book1
Title of Book 1
Fantasy
Book2
Not the Title of Book 1
Fantasy
####
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;
my $DATA = '
Book1
Title of Book 1
Fantasy
Book2
Not the Title of Book 1
Fantasy
';
my $source_twig = XML::Twig->new('pretty_print' => 'indented');
$source_twig->safe_parse($DATA);
foreach my $book ($source_twig->root->children('book')) {
if ($book->first_child('title')->text() ne 'Title of Book 1') {
$book->cut()
}
}
$source_twig->print();
####
Book1
Title of Book 1
Fantasy