Thanks a lot to all. The code provided by you was working as per my requirement. I am from Java background from past 9 years, recently took up work on Perl. As I am new to Perl, I am not very familiar with RegEx.. trying to learn :) Thanks a lot for your time and help. I had tried to do using LibXML. Here is my code... Even I added sort functionality. But it is incomplete,, commented the sort part.

#!/usr/bin/perl use strict; use warnings; use strict; use XML::LibXML; my ($inputfile,$struct,$outputfile,$parser) indentXML(); sortOnPUI(); sub indentXML { $inputfile = 'OpsbankII_10_items.xml'; $parser = XML::LibXML->new(); $struct = $parser->parse_file($inputfile); #print $struct->toString(2); $outputfile="output.xml"; print $struct->toFile($outputfile,2); } sub sortOnPUI { my $rootel = $struct -> getDocumentElement(); my $rootelname = $rootel -> getName(); print "Root element is a $rootelname\n"; my @items = $rootel->getElementsByTagName('item'); foreach my $item (@items) { my @itemids = $item->getElementsByTagName('itemid'); foreach my $itemid (@itemids){ #print "itemid content is ",$itemid->textContent,"\n"; my @attrs= $itemid->attributes; foreach my $at (@attrs) { #print "itemid attribute content is ",$at->textContent,"\n"; if ($at->textContent eq "PUI") { print "Attribute is ", $at->textContent," and value is " +, $itemid->to_literal," \n"; # my @sorted_class_elems # = sort { $a->findvalue('itemid') <=> $b->findvalue( +'itemid') } # @items; }#if }#foreach }#foreach }#foreach }

In reply to Re^3: Sort xml based on attribute (use a parser) by bharathinc
in thread Sort xml based on attribute 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.