Hi,

I am trying to add attributes to a tag as per my requirement. But in the result It is not in the same order as I added attributes. Please let me know how can I add attributes and get the result in the same order. My code is as below. Thank you in advance.

#!/usr/bin/perl use strict; use warnings; use XML::Twig; my $tagname="ait:date-delivered"; my $att1="month"; my $att2="year"; my $att3="day"; changeTagAttrOrder(); sub changeTagAttrOrder { my $t= XML::Twig->new( output_filter=>'safe', twig_handlers => { '_default_' => sub { }, *[@att='type'] => \&changeTagAttrOrderH +andler; }, pretty_print => 'indented', keep_atts_order => 1,#Tie::IxHash needs to be installed fo +r this option to be available. ); $t->parse(\*DATA); $t->print; $t->purge; }#end of changeTagAttrOrder sub changeTagAttrOrderHandler { my( $t,$rootElt)= @_; my $curEltPath = $rootElt->path; if($curEltPath =~ m/$tagname/){ print "Found tag $tagname in path $curEltPath\n"; print $rootElt->att($att1)."\t".$rootElt->att($att2)."\t".$roo +tElt->att($att3); my $att1Val=$rootElt->att($att1); my $att2Val=$rootElt->att($att2); my $att3Val=$rootElt->att($att3); $rootElt->del_atts; print "after delete \tAttributes are ::".join(",",$rootElt->a +tt_names())."\n"; $rootElt->set_atts( { $att1 => $att1Val, $att2 => $att2Val, $a +tt3 => $att3Val}); print "after new set \tAttributes are ::".join(",",$rootElt->a +tt_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>

In reply to 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.