I would say that your example is probably over simplistic, so I've expanded on and cleaned it up slightly:
<library>
<book>
<book1>Book1</book1>
<title>Title of Book 1</title>
<genre>Fantasy</genre>
</book>
<book>
<book2>Book2</book2>
<title>Not the Title of Book 1</title>
<genre>Fantasy</genre>
</book>
</library>
Now that we have something that's a little bit easier to show off some stuff, here's an example that does exactly what you ask.
use strict;
use warnings;
use XML::Twig;
use Data::Dumper;
my $DATA = '
<library>
<book>
<book1>Book1</book1>
<title>Title of Book 1</title>
<genre>Fantasy</genre>
</book>
<book>
<book2>Book2</book2>
<title>Not the Title of Book 1</title>
<genre>Fantasy</genre>
</book>
</library>
';
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();
And it's output:
<library>
<book>
<book1>Book1</book1>
<title>Title of Book 1</title>
<genre>Fantasy</genre>
</book>
</library>
|