#!/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 ); }