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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |