I've got a simple xml document ...
<?xml version="1.0" encoding="UTF-8" ?> <charge-type-control> <charge code="FRT" name="frtamt"/> <charge code="SV1" name="savamt"/> <charge code="SV2" name="savamt"/> <charge code="SV3" name="savamt"/> <charge code="SV4" name="savamt"/> <charge code="SV5" name="savamt"/> </charge-type-control>
that I would like to parse into the data structure ...
{ 'SV1' => 'savamt', 'SV5' => 'savamt', 'SV2' => 'savamt', 'FRT' => 'frtamt', 'SV3' => 'savamt', 'SV4' => 'savamt' };
However, the closest I've been able to get with XML::Simple is ...
{ 'charge' => { 'SV1' => { 'name' => 'savamt' }, 'SV5' => { 'name' => 'savamt' }, 'SV2' => { 'name' => 'savamt' }, 'SV4' => { 'name' => 'savamt' }, 'SV3' => { 'name' => 'savamt' }, 'FRT' => { 'name' => 'frtamt' } } };
using this XMLin line ...
$ref = XMLin('charge-type-control.xml', KeyAttr => ['code']);
the docs allude to using ValueAttr like...
$ref = XMLin('charge-type-control.xml', KeyAttr => ['code'], ValueAttr + => ['name']);

but the results are the same. So either it doesn't do what I think it does or what I want to do is impossible. Can I coerce XML::Simple to do exactly what I want or do I have to write my own transformer like...

my %myref; for my $key (keys %{$ref->{charge}}) { $myref{$key} = $ref->{charge}->{$key}->{name}; }
to get exactly what I want?

In reply to XML::Simple help by mifflin

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.