Thank you Dear, your coding is fentastic. This solution is working. Even I did my code as below. But my code was specific to elements.Here is my code
#!/usr/bin/perl use strict; use warnings; use XML::Twig; my (@sortedM, @sortedL); my $i=0; my $twig= new XML::Twig(twig_handlers => { 'enhancement' => sub {$_->first_child- +>sort_children_on_att("type");}, 'descriptor' => \&sortSubEltsHandler, }, pretty_print => 'indented', keep_atts_order => 1, ); $twig->parse(\*DATA); $twig->print; $twig->purge; sub sortSubEltsHandler { my( $t,$elt)= @_; my $root= $t->root; my @mainterms = $root->descendants(q{mainterm}); @sortedM = reverse sort { $a->text cmp $b->text } @mainterms; my @links = $root->descendants(q{link}); @sortedL =reverse sort { $a->text cmp $b->text } @links; $elt->cut_children; foreach my $mainterm (@sortedM) { my $new_elt= XML::Twig::Elt->new($mainterm->name => $mainterm->tex +t); $new_elt->paste($elt); } foreach my $link (@sortedL) { my $new_elt= XML::Twig::Elt->new($link->name => $link->text); $new_elt->paste($elt); } }#end of sortSubEltsHandler __DATA__ <enhancement> <descriptorgroup> <descriptors type="MED" controlled="y"> <descriptor> <mainterm weight="a">plague</mainterm> <mainterm>disease</mainterm> <mainterm>pestilence</mainterm> <link>f</link> <link>C</link> <link>M</link> </descriptor> </descriptors> <descriptors type="DRG" controlled="y"> <descriptor> <mainterm>panadol</mainterm> <mainterm weight="a">zorro</mainterm> <mainterm>xenon</mainterm> <mainterm weight="a">aspirin</mainterm> <link>b</link> <link>P</link> <link>z</link> </descriptor> </descriptors> </descriptorgroup> </enhancement>
In reply to Re^4: twig inbuilt methods to sort whole xml tree
by bharathinc
in thread twig inbuilt methods to sort whole xml tree
by bharathinc
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |