#!/usr/bin/perl -w use strict; use XML::LibXML; my $parser = XML::LibXML->new(); my $doc = $parser->parse_string(join "", ); # copy the node, then delete it my $from_path = q{/category/subcategory/product[product_id = "800.55.01"]}; my @from_nodes = $doc->findnodes( $from_path); my $dead = $from_nodes[0]; # Assume there can be only one $dead->unbindNode; # paste the node to a new location my $to_path = q{/category/subcategory[@name = "Grand Arabica"]}; my @to_nodes = $doc->findnodes( $to_path); my $paste = $to_nodes[0]; # Assume there can be only one my $lastnode = $paste->lastChild(); $paste->insertAfter( $dead, $lastnode); print $doc->toString([0]);