I followed what I said to you, and here is the code, and result:

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;
<subcategory> <Grand Arabica> <photo>images/arabica.jpg</photo> <product> <brand>Legal</brand> <channel>A</channel> <channel>D</channel> <channel>S</channel> <conditionning> <content>250</content> <unit>gr</unit> </conditionning> <content>Coffee </content> <description>Grand Arabica</description> <packaging_qty>12</packaging_qty> </product> <product_id>802.55.01</product_id> </Grand Arabica> <Prestige> <photo>images/prestige.jpg</photo> <product> <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <conditionning> <content>250</content> <unit>gr</unit> </conditionning> <content>Coffee </content> <description>Legal Prestige</description> <packaging_qty>12</packaging_qty> <recipe>/recipes/coffee1.html</recipe> </product> <product> <brand>Legal</brand> <channel>D</channel> <channel>A</channel> <conditionning> <content>500</content> <unit>gr</unit> </conditionning> <content>Coffee </content> <description>Legal Prestige Boite Arôme</description> <packaging_qty>6</packaging_qty> </product> <product_id>800.55.01</product_id> </Prestige> </subcategory>

In reply to Re^2: XML cut and paste by pg
in thread XML cut and paste by Maxim

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.