Uma maneira de usar perl
com a ajuda do XML::Twig
parser:
#!/usr/bin/env perl
use strict;
use warnings;
use XML::Twig;
use File::Spec;
my $twig = XML::Twig->new(
twig_handlers => {
## For each 'img' tag execute following function...
'img' => sub {
## If it doesn't have an 'alt' attribute...
if ( ! $_->att_exists( 'alt' ) ) {
## Get value of 'src' tag.
my $src = $_->att( 'src' );
return unless $src;
## Get last part of the path and remove extension.
my $src_file = (File::Spec->splitpath( $src ))[2] || q{};
$src_file =~ s/\.[^.]+$//;
## Create the 'alt' attribute.
$_->set_att( 'alt', $src_file );
}
}
},
pretty_print => 'indented',
)->parsefile( shift )->print;
Execute-o com seu arquivo xml
como argumento exclusivo, como:
perl script.pl xmlfile