#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $twig = new XML::Twig( pretty_print => 'record_c' ); $twig->parsefile('1.xml'); for my $fig ( $twig->get_xpath('//fig') ) { my $label = $fig->get_xpath('./label', 0); my $caption = $fig->get_xpath('./caption', 0); next unless $label and $caption; $label->cut(); $label->paste('first_child', $caption); } $twig->print;