Perl sort xml files elements

Hi

I have a xml file and I need to sort some of the elements in it and can't figure out how to do it.

My structure is

<job>
      <OUTCOND NAME="AAP10151_AAP10221_AAP10151_AAP10122" ODATE="ODAT" SIGN="-"/>
      <OUTCOND NAME="AAP10151_AAP10224_AAP10151_AAP10122" ODATE="ODAT" SIGN="-"/>
      <OUTCOND NAME="AAP10151_AAP10223_AAP10151_AAP10122" ODATE="ODAT" SIGN="-"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AAB10021" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AAP10931" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AA020931" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10181_AAP10181" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_SINGLES_CY300035" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_LP408397_LP408725" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AAB10021" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AAP10931" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AA020931" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10181_AAP10181" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_SINGLES_CY300035" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_LP408397_LP408725" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AAB10021" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AAP10931" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10151_AA020931" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_AAP10181_AAP10181" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_SINGLES_CY300035" ODATE="ODAT" SIGN="+"/>
      <OUTCOND NAME="AAP10151_AAP10122_LP408397_LP408725" ODATE="ODAT" SIGN="+"/>
</job>

and I need to sort in the file the OUTCOND then by NAME then by ODATE and then by SIGN

Not sure if there is a way to do this using XML::Twig or XML::LibXML

Thanks

Mike
LVL 1
mikeysmailbox1Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Flabio GatesCommented:
I only dabble in perl once in a blue moon but I cobbled this based on code from stackoverflow.
use strict;
use warnings 'all';

use XML::LibXML;
use XML::LibXML::PrettyPrint;

my $doc = XML::LibXML->load_xml(location => 'unsorted.xml');

my ($objects) = $doc->findnodes('/job');

my @items = $objects->findnodes('*');
@items = sort {
    $a->getAttribute('NAME') cmp $b->getAttribute('NAME')
    ||
    $a->getAttribute('ODATE') cmp $b->getAttribute('ODATE')
    ||
    $a->getAttribute('SIGN') cmp $b->getAttribute('SIGN')
} @items;

$objects->removeChildNodes;
$objects->appendChild( $_ ) for @items;

my $pp = XML::LibXML::PrettyPrint->new(indent_string => "  ");
$pp->pretty_print($doc);
print $doc;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Flabio GatesCommented:
Question inactive
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
XML

From novice to tech pro — start learning today.