bharathinc has asked for the wisdom of the Perl Monks concerning the following question:
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>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: set attributes to an element in user defined order
by ambrus (Abbot) on Mar 05, 2010 at 07:31 UTC | |
|
Re: set attributes to an element in user defined order
by NiJo (Friar) on Mar 02, 2010 at 19:11 UTC | |
by ambrus (Abbot) on Mar 03, 2010 at 14:23 UTC | |
|
Re: set attributes to an element in user defined order
by stefbv (Priest) on Mar 05, 2010 at 07:30 UTC | |
|
Re: set attributes to an element in user defined order
by Jenda (Abbot) on Mar 03, 2010 at 08:43 UTC | |
by ambrus (Abbot) on Mar 03, 2010 at 14:12 UTC |