sureerat has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm using XML::Twig to manipulate my xml data and it's working fine. But I get new requirement to order my xml records. I doubt whether XML::Twig can find the correct position to add record so the data is properly ordered by "name" alphabetically OR I must add it in random position and sort it then.

This is the further coding for my last script at http://www.perlmonks.org/index.pl?node_id=719528, Thank you Tanktalus to help revise my coding.

#!/usr/bin/perl -w use strict; use XML::Twig; my %links = ( "img" => "/images/Orange_Test.jpg", "alt" => "Orange Test Image", "name" => "Orange Test Image", "title" => "Hello World. This is a test page. Please clic +k here for more information.", "url" => "http://localhost/orange_test.htm", "dcr" => "/templatedata/test2123/test/orange_test", ); my $path = "/orange_test.htm"; my $file = "s1.xml"; my $x = XML::Twig::Elt->new( record => map { XML::Twig::Elt->new( $_ => $links{$_}) } sort keys %links) ->set_att("path",$path); my $twig = XML::Twig->new(pretty_print => 'indented'); if(!-s $file) { $twig->set_root( XML::Twig::Elt->new(data => $x) ); } else { my $item_pasted; $twig->setTwigHandlers( { record => sub { my( $twig, $record)= @_; unless ($item_pasted) { my $rpath = $record->att('path' +); if ($rpath eq $path) { $x->replace($record); $item_pasted=1; return } } }, } ); $twig->parsefile($file); $x->paste('last_child',$twig->root) unless($item_pasted); } { open (my $xfile, ">", $file) or die "Can't save to $file: $!"; $twig->print(\*$xfile); }
and the xml file is here:
<data> <record path="/blue_test.htm"> <alt>Blue Test</alt> <dcr>/templatedata/test2123/test/blue_test</dcr> <img>/images/Blue_Test.jpg</img> <name>Blue Test</name> <title>Hello World. This is a test page. Please click here for mor +e information.</title> <url>http://localhost/blue_test.htm</url> </record> <record path="/yellow_test.htm"> <alt>Yellow Test</alt> <dcr>/templatedata/test2123/test/yellow_test</dcr> <img>/images/Yellow_Test.jpg</img> <name>Yellow Test</name> <title>Hello World. This is a test page. Please click here for mor +e information.</title> <url>http://localhost/yellow_test.htm</url> </record> </data>
Thank you in advance.

Replies are listed 'Best First'.
Re: use XML::Twig to insert data in the correct order
by mirod (Canon) on Nov 09, 2008 at 06:54 UTC

    I don't really see the problem here. You can perfectly insert the new record element in the right position, as long as the initial record set is sorted: that's just before the first record with a name greater than the inserted record name.

    So add the following code to the handler, and voilą!

    elsif( $record->field( 'name') g +e $x->field( 'name')) { $x->paste( before => $recor +d); $item_pasted=1; return }
      Hello Mirod,

      Thank you for your suggestion. It's working well. I like XML::Twig, it's a great perl module. ^_^

Re: use XML::Twig to insert data in the correct order
by Anonymous Monk on Nov 08, 2008 at 13:13 UTC
    its possible, but isnt that a job for stylesheets XSL
      So it means I can add data without ordering but order the data when query, right? It's also in my plan. This is my trial to display data in order.
      <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <table border="2" bgcolor="white"> <tr> <th>Name</th> <th>URL</th> </tr> <xsl:for-each select="data/record" order-by="+ name"> <tr> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="url"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
      But I would like to display data in "select" menu in my web form. I have no idea how to order the data with XML before create the "select" field. Please give me the clue. Thank you.