#!/usr/bin/perl -w use strict; use XML::Rules; use Date::Calc qw(Add_Delta_YM Decode_Date_EU Delta_Days Delta_YMD); use Data::Dumper; my @now = (localtime(time))[5, 4, 3]; $now[0] += 1900; # Perl years start in 1900 $now[1]++; # months are zero-based my $parser = XML::Rules->new( rules => [ price => sub { price => fixPrice( $_[1]->{_content}, 0.20 )}, dob => sub {age => dob2age( $_[1]->{_content})}, 'name,owner' => 'content', 'dog,cat,hippo' => sub {delete($_[1]->{_content}); $_[0] => $_[1]}, ], style => 'filter', ); $parser->filterfile('pets.xml', \*STDOUT); # ... and the functions defined in the article #### #!/usr/bin/perl -w use strict; use XML::Rules; use Date::Calc qw(Add_Delta_YM Decode_Date_EU Delta_Days Delta_YMD); use Data::Dumper; my @now = (localtime(time))[5, 4, 3]; $now[0] += 1900; # Perl years start in 1900 $now[1]++; # months are zero-based my $parser = XML::Rules->new( rules => [ price => sub { price => fixPrice( $_[1]->{_content}, 0.20 )}, dob => sub {age => dob2age( $_[1]->{_content})}, 'name,owner' => 'content', '_default' => 'no content array', 'pets' => sub { my ($tag, $attrs) = @_; delete($attrs->{_content}); foreach my $kind (values %{$attrs}) { next unless ref($kind) eq 'ARRAY'; # just in case $kind = [sort {$a->{name} cmp $b->{name}} @$kind]; # sort the animals of a kind by name }; return $tag => $attrs; }, ], style => 'filter', ident => ' ', ); $parser->filterfile('pets.xml', \*STDOUT); # ... and the functions (78 lines in total)