The problem is that the

my $e_parent = $t->findnodes($parent);
doesn't return anything. It does look a bit strange, but I don't know XML::Twig enough to be able to suggest the correct syntax. This is how the solution would look with XML::Rules:
use XML::Rules; my $parser = XML::Rules->new( style => 'filter', ident => ' ', rules => { _default => 'as array trim', elt => sub {delete $_[1]->{_content};$_[0] =>$_[1]}, 'elt_att,selt_att' => sub {$_[0] => $_[1]->{att}}, } ); $parser->filter(\*DATA); __DATA__ <doc> <elt elt_class="class1"> <subelt subelt_class="sclass1"> <content id="content1"/></subelt +> <elt_att att="elt_att1"></elt_att> </elt> <elt elt_class="class2"> <subelt subelt_class="sclass2"><content id="content2"/></subelt> <selt_att att="selt_att1"></selt_att> </elt> <elt elt_class="class3"> <subelt subelt_class="sclass3"><content id="content3"/></subelt> <elt_att att="elt_att1"></elt_att> </elt> </doc>

The rule for the <elt> is necessary because XML::Rules in the filter mode copies everything it parses as is until it encounters a tag with a custom rule so if there was no special rule for <elt> the module would copy the opening <elt> tag, the <subelt> tag with contents and then would process the <elt_att> and be unable to return back to the <elt> to add the results of the <elt_att> rule to the <elt> tag and would create a <elt_att>elt_att1</elt_att> tag.


In reply to Re: XML::Twig creating generic subroutine for attributes by Jenda
in thread XML::Twig creating generic subroutine for attributes by bladestonight

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.