#### HAND EDITED, NOT TESTED #### #### HAND EDITED, NOT TESTED #### #### HAND EDITED my $t = XML::Twig->new( twig_handlers => { b => \&b, }, pretty_print => 'indented', ); $t->parsefile($xml_file); $t->flush; sub b { my ( $t, $x ) = @_; my @c = $x->children('c'); my %bs; foreach my $c (@c) { my $text = $c->att('name'); $c->cut; push ( @{ $bs{$text} }, $c ); } foreach my $text ( keys %bs ) { my $b = $x->insert_new_elt( 'after', 'b', { %{ $x->atts } } ); $b->set_att( 'name' => $text ); foreach ( @{ $bs{$text} } ) { $adg->insert_new_elt( 'first_child', 'c', { %{ $_->atts } } ); } } $x->delete; } }