in reply to Re: Twig delete not deleting the entire section?
in thread Twig delete not deleting the entire section?

That would be a straight up bug, wouldn't it? But I'm not sure that's right. I think we may be missing something because first it does delete the closing /site tag, plus, this below should have worked then because I'm specifically parsing the children -
use strict; use warnings; use XML::Twig; my $xml = q( <sites> <site siteid="ONE"> <name>name1</name> <address>address1</address> <contact>contact1</contact> </site> <site siteid="TWO"> <name>name2</name> <address>address2</address> <contact>contact2</contact> </site> </sites> ); my %handlers = ( 'name[string() =~ /name2/]' => sub { my ($twig, $cnt) = @_; my $parent = $cnt->parent; foreach ($parent->children) { print "Deleting: " . $_->text . "\n"; $_->delete; } $parent->delete; } ); my $twig= new XML::Twig( PrettyPrint => 'indented', twig_handlers => + \%handlers); $twig->parse($xml); print $twig->sprint;
Gave the following output -
Deleting: name2 <sites> <site siteid="ONE"> <name>name1</name> <address>address1</address> <contact>contact1</contact> </site> <address>address2</address> <contact>contact2</contact> </sites>

Replies are listed 'Best First'.
Re^3: Twig delete not deleting the entire section?
by poj (Abbot) on Mar 14, 2018 at 19:02 UTC

    When the handler triggers on name only

    <site siteid="TWO"> <name>name2</name>

    has been parsed so name is the only child the parent has.

    Add a $cnt->parent->print statement to see it. If you change the order of elements your original code works.

    <sites> <site siteid="ONE"> <name>name1</name> <address>address1</address> <contact>contact1</contact> </site> <site siteid="TWO"> <address>address2</address> <contact>contact2</contact> <name>name2</name> </site> </sites>

    If the handler is on site like choroba said, that is triggered after all the children have been parsed.

    poj