How move figure entity to splited files

R

Rahul

Hi All,

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.

My input xml
-------------
<!DOCTYPE document SYSTEM "rr.dtd"[
<!ENTITY A3_11_f01 SYSTEM "A3_11_f01.tif" NDATA tif>
<!ENTITY A3_12_f01 SYSTEM "A3_12_f01.tif" NDATA tif>
]>
<document>
<chapter id="ch11"><title>chapter 11</title>
<p>some test</p>
<figure>
<graphic picfile="A3_11_f01"/>
<caption>adjkf</caption>
</figure>
</chapter>
<chapter id="ch12"><title>chapter 12</title>
<p>some test</p>
<figure>
<graphic picfile="A3_12_f01"/>
<caption>adjkf</caption>
</figure>
</chapter>
</document>

I want my output
-----------------
chapter11.xml
_____________

<!DOCTYPE document SYSTEM "rr.dtd"[
<!ENTITY A3_11_f01 SYSTEM "A3_11_f01.tif" NDATA tif>
]>
<document>
<chapter id="ch11"><title>chapter 11</title>
<p>some test</p>
<figure>
<graphic picfile="A3_11_f01"/>
<caption>adjkf</caption>
</figure>
</chapter>
</document>

================

chapter12.xml
___________

<!DOCTYPE document SYSTEM "rr.dtd"[
<!ENTITY A3_12_f01 SYSTEM "A3_12_f01.tif" NDATA tif>
]>
<document>
<chapter id="ch12"><title>chapter 12</title>
<p>some test</p>
<figure>
<graphic picfile="A3_12_f01"/>
<caption>adjkf</caption>
</figure>
</chapter>

I am using window base perl. Please anyone help.

Thanks
Byomokesh
 
M

mirod

Rahul said:
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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top