Rahul wrote:
> I have one xml file. My task is split xml file in Chapter wise and
> Figure entity notation also move to same chapter. My problem is how
> move entity notation.
> ...
Here is a solution using XML::Twig. Note that the file names are
generated from the id attribute of the file (as ch11.xml, ch12.xml...)
which may or may not be a good idea.
OTH
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my @graphics_in_chapter; # global, could be passed around, but in
# such a short script I did not see the point
XML::Twig->new( twig_handlers => { chapter => \&dump_chapter ,
graphic => \&store_graphic,
},
pretty_print => 'indented',
)
->parsefile( shift( @ARGV));
# store the entity name
sub store_graphic
{ my( $t, $graphic)= @_;
push @graphics_in_chapter, $graphic->att( 'picfile');
}
sub dump_chapter
{ my( $t, $chapter)= @_;
my $id= $chapter->id;
open( my $out, '>:utf8', "$id.xml") or die "cannot create '$id.xml':
$!";
# output the doctype, including the entities found in the chapter
printf {$out} qq{<!DOCTYPE %s SYSTEM "%s" [ \n%s\n]>\n},
$t->doctype_name, $t->system_id,
entities( $t, @graphics_in_chapter);
$t->root->print( $out); # printing the root outputs the document tag
$chapter->delete; # so we have at most 1 chapter in memory
@graphics_in_chapter=(); # reset the global
}
# return the text of entity declarations in @entity_names
sub entities
{ my( $t, @entity_names)= @_;
# list of entity objects found in the chapter
my @entities= map { $t->entity( $_)->sprint } @entity_names;
return join "\n", @entities;
}
--
mirod
|