use XML::Simple; use Data::Dumper; use strict; use warnings; my $ref = XMLin("test1.xml"); my $subcats = $ref->{'subcategory'}; for my $subcat (keys(%$subcats)) { my $id; my $products = $subcats->{$subcat}->{'product'}; if (ref($products) eq 'ARRAY') { $id = $products->[0]->{'product_id'}; for (0 .. $#{@$products}) { delete $products->[$_]->{'product_id'}; } } elsif (ref($products) eq 'HASH') { $id = $products->{'product_id'}; delete $products->{'product_id'}; } $subcats->{$subcat}->{'product_id'} = $id; } my $xml = XMLout($ref, KeepRoot=>1, NoAttr => 1); print $xml; #### images/arabica.jpg Legal A D S 250 gr Coffee Grand Arabica 12 802.55.01 images/prestige.jpg Legal D A 250 gr Coffee Legal Prestige 12 /recipes/coffee1.html Legal D A 500 gr Coffee Legal Prestige Boite Arôme 6 800.55.01