use strict; use warnings; use XML::Twig; my $content = do {local $/; }; my $twig = XML::Twig->new(pretty_print => 'nice'); $twig->parse($content); my @first = $twig->get_xpath('//one'); my @elts = $twig->get_xpath('//two'); push @elts, $twig->get_xpath('//three'); my $group = XML::Twig::Elt->new ('group'); $group->paste ('after', $first[0]); $_->move ('last_child', $group) for @elts; $content = $twig->sprint; print $content; __DATA__ some text some text some text more text