in reply to extract tag form xml file

Since various other solutions have been posted, and you haven't been exactly clear in your requirements, here I use a parser, Mojo::DOM, and Mojo::File for reading/writing. There's more than one way to do it, other parsers exist (XML::Twig etc)...

source.xml

<inline-formula id="ieqn-1"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula> <inline-formula id="ieqn-2"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula> <inline-formula id="ieqn-3"><alternatives><mml:math display="inline"><mml:mi>&#964;</mml:mi></mml:math></alternatives></in +line-formula>

1226654.pl

#!/usr/bin/perl use strict; use warnings; use Mojo::DOM; use Mojo::File; # read the xml file my $source = Mojo::File->new( 'source.xml' ); my $xml = $source->slurp; # use Mojo::Dom to parse the XML my $dom = Mojo::DOM->new->xml(1)->parse( $xml ); # for each inline-formula id in the file for my $e ( $dom->find('inline-formula[id]')->each ){ # create file named value_of_id.mml my $file = "$e->{id}.mml"; my $path = Mojo::File->new( $file ); # write contents to file $path->spurt( $e->to_string ); }

output

-rw-rw-r-- 1 marto marto 131 Dec 4 10:17 ieqn-3.mml -rw-rw-r-- 1 marto marto 131 Dec 4 10:17 ieqn-2.mml -rw-rw-r-- 1 marto marto 131 Dec 4 10:17 ieqn-1.mml marto@Shemp:~/code/perlmonks$ cat ieqn-1.mml <inline-formula id="ieqn-1"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula> marto@Shemp:~/code/perlmonks$ cat ieqn-2.mml <inline-formula id="ieqn-2"><alternatives><mml:math display="inline">< +mml:mi>&#964;</mml:mi></mml:math></alternatives></inline-formula>