Selvakumar has asked for the wisdom of the Perl Monks concerning the following question:

The attributes sorted by ascending order by default even if i am using keep_atts_order. How can i keep the required attributes in the same order which i have gievn in the set_atts command.
I need the order like bid, aid, att, pgnbr etc.,
use XML::Twig; my $xmlfile = 'check.xml'; my $twig=XML::Twig->new( twig_handlers=>{ keep_atts_order=>1, color=>\&content_handler}, pretty_print => 'indented', ); $twig->parse(\*DATA); sub content_handler { my ($twig, $color)=@_; $color->del_atts; $color->set_atts({bid=>$bidvar, aid=>$aidvar, att=>'', pgnbr=>$pgnbrva +r, pgsect=>$pgsectvar, spp=>'', spf=>$spfval, top=>$topvar, left=>$le +ftvar, height=>$heightvar, bh=>$bhvar, width=>$widthvar, bw=>$bwvar, +colums=>$columsvar, gutter=>$guttervar, ts=>$tsvar, ls=>$lsvar, bs=>$ +bsvar, rs=>$rsvar, t=>$tvar, l=>$lvar, b=>$bvar, r=>$rvar, tit=>$titv +ar, til=>$tilvar, tib=>$tibvar, tir=>$tirvar, color=>$colorvar, cn=>$ +cnvar, framewidth=>$framewidthvar, framecolor=>$framecolorvar, fcn=>$ +fcnvar, grp=>$grpvar}); } $twig->print; __DATA__ <?xml version="1.0"?> <document> <root> <box type="text" cont="text" textflowid="rc_u5035" frameColorID="ua +5"/> </root> <content type="text" Self="rc_u5035" id="rc_u5035"/> <header>h3</header> <meta> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_15_D_50_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 04-15M/50Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="uce"/> <color type="colr" clmd="prss" clsp="CMYK" clvl="4_D_0_D_10_D_40_D_0 +" ovrd="norm" atcs="nasp" atvl="0" pnam="Color 05-10M/40Y" edbl="t" r +mbl="t" pvis="t" swID="1f01" Self="ua5"/> </meta> </document>
CURRENT OUTPUT
<?xml version="1.0"?> <document> <root> <box cont="text" frameColorID="ua5" textflowid="rc_u5035" type="te +xt"/> </root> <content Self="rc_u5035" id="rc_u5035" type="text"/> <header>h3</header> <meta> <color aid="" att="" b="" bh="" bid="" bs="" bw="" cn="" color="" +colums="" fcn="" framecolor="" framewidth="" grp="" gutter="" height= +"" l="" left="" ls="" pgnbr="" pgsect="" r="" rs="" spf="" spp="" t=" +" tib="" til="" tir="" tit="" top="" ts="" width=""/> <color aid="" att="" b="" bh="" bid="" bs="" bw="" cn="" color="" +colums="" fcn="" framecolor="" framewidth="" grp="" gutter="" height= +"" l="" left="" ls="" pgnbr="" pgsect="" r="" rs="" spf="" spp="" t=" +" tib="" til="" tir="" tit="" top="" ts="" width=""/> </meta> </document>

Replies are listed 'Best First'.
Re: xml::twig attributes
by ikegami (Patriarch) on Oct 22, 2009 at 05:22 UTC
    my $twig = XML::Twig->new( twig_handlers => { keep_atts_order => 1, color => \&content_handler, }, pretty_print => 'indented', );

    should be

    my $twig = XML::Twig->new( twig_handlers => { color => \&content_handler, }, keep_atts_order => 1, pretty_print => 'indented', );

    You should have seen that problem if you had indented properly. Boo for bad indenting!

    Furthermore, by placing the atts in a hash before passing them to set_atts, you change their order before the module even sees them. Turns out set_atts accepts a list of key=>val pairs as an alternative to a hash ref, so change

    $color->set_atts({...});

    to

    $color->set_atts(...);

    I hope this is just for æsthetics. You shouldn't expect attributes to be in a particular order.

Re: xml::twig attributes
by Anonymous Monk on Oct 22, 2009 at 05:28 UTC
    Do you have Tie::IxHash ? Maybe you shouldn't ->del_atts