in reply to Sort n Create XML

Purists would say, this is a job for XSLT. But for me that language is terribly mindboggling. So if I face XML-transformations I normally settle on this approach: This has the charme that it nicely separates presentation from logic.

Example:
use strict; use warnings; use Template; use XML::Simple; my $data = XMLin( $ARGV[0], ForceArray => 'class' ); $data->{class} = [ sort { $a->{strength}->[0] <=> $b->{strength}->[0] } @{$data->{class}} ]; #use Data::Dumper; #print Dumper ( $data ); my $tt = Template->new(); $tt->process( \*DATA, {data => $data}, $ARGV[1] ) || die $tt->error(), "\n"; __DATA__ <?xml version="1.0" encoding="UTF-8"?> <system> <header></header> [% FOR class = data.class %] <class> <name>[% class.name.0 %]</name> <strength>[% class.strength.0 %]</strength> </class> [% END %] <footer></footer> </system>
You can call this as with two arguments. The first is the file to be read, the second is the file to be written. If you omit the second argument, the output is written to STDOUT.


holli, /regexed monk/