I made the code to run but it doesn't work :)

I mean that it doesn't do what it's suppose to do, this is the output:

after delete Attributes are: after new set Attributes are: day,month,year <item> ... <ait:date-delivered month="02" day="09" year="2010"/> ... </item>

It's funny but the order is different for all tree steps, in the original doc, aftet delete and insert an in the resulting doc.

On the other hand the man page say:
'keep_atts_order' allows outputting the attributes in the same order as they were in the original document.

Does this mean XML::Twig it's not designed to work this way?

#!/usr/bin/perl use strict; use warnings; use XML::Twig; changeTagAttrOrder(); sub changeTagAttrOrder { my $t = XML::Twig->new( output_filter=>'safe', twig_handlers => { 'ait:date-delivered' => \&changeTagAttrOrderHandler, }, pretty_print => 'indented', keep_atts_order => 1, ); $t->parse(\*DATA); $t->print; $t->purge; }#end of changeTagAttrOrder sub changeTagAttrOrderHandler { my( $t,$rootElt)= @_; my $curEltPath = $rootElt->path; my $att1Val = $rootElt->att('month'); my $att2Val = $rootElt->att('year'); my $att3Val = $rootElt->att('day'); $rootElt->del_atts; print "after delete\n Attributes are: ".join(',',$rootElt->att_names())."\n"; # $rootElt->set_atts( # { month => $att1Val, year => $att2Val, day => $att3Val}); $rootElt->set_att(day => $att3Val); $rootElt->set_att(month => $att1Val); $rootElt->set_att(year => $att2Val); print "after new set\n Attributes are: ".join(',',$rootElt->att_names())."\n"; }#end of changeTagAttrOrderHandler __DATA__ <item> <ait:process-info> <ait:date-delivered year="2010" month="02" day="09"/> <ait:date-sort year="2008" month="1" day="1"/> <ait:status type="core" state="new" stage="S300"/> </ait:process-info> </item>

Regards, Stefan


In reply to Re: set attributes to an element in user defined order by stefbv
in thread set attributes to an element in user defined order by bharathinc

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.