Hi, I would like to sort whole xml tree i.e attribute as well as on text of all sub elements. I have attached my code below. But the problem here is... attribute sort is fine, where as the sub element sorting has done only for the first set. Could you please help me to resolve this ASAP. It will be of great help. Thank you in advance

#!/bin/perl -w use strict; use XML::Twig; my $twig= XML::Twig->new(pretty_print=>'indented'); $twig->parse(\*DATA); my $root=$twig->root; $twig->root->first_child->sort_children( sub { $_->{'att'}->{"type"}}) +; $twig->root->first_child->first_child->sort_children( sub { $_->text}) +; #$twig->root->sort_children_on_att("type"); #$twig->root->sort_children_on_field('mainterm'); $twig->print; __DATA__ <enhancement> <descriptorgroup> <descriptors type="MED" controlled="y"> <descriptor> <mainterm weight="a">plague</mainterm> <mainterm>disease</mainterm> <mainterm>pestilence</mainterm> </descriptor> </descriptors> <descriptors type="DRG" controlled="y"> <mainterm>panadol</mainterm> <mainterm weight="a">zorro</mainterm> <mainterm>xenon</mainterm> <mainterm weight="a">aspirin</mainterm> </descriptors> </descriptorgroup> </enhancement>

output I got was as below

---------- perl compile ---------- <enhancement> <descriptorgroup> <descriptors controlled="y" type="DRG"> <mainterm weight="a">aspirin</mainterm> <mainterm>panadol</mainterm> <mainterm>xenon</mainterm> <mainterm weight="a">zorro</mainterm> </descriptors> <descriptors controlled="y" type="MED"> <descriptor> <mainterm weight="a">plague</mainterm> <mainterm>disease</mainterm> <mainterm>pestilence</mainterm> </descriptor> </descriptors> </descriptorgroup> </enhancement> Output completed (3 sec consumed) - Normal Termination

In reply to twig inbuilt methods to sort whole xml tree 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.