Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How move figure entity to splited files

Reply
Thread Tools

How move figure entity to splited files

 
 
Rahul
Guest
Posts: n/a
 
      05-16-2007
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

 
Reply With Quote
 
 
 
 
mirod
Guest
Posts: n/a
 
      05-16-2007
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Entity, problem with entity key ThatsIT.net.au ASP .Net 1 09-07-2009 02:20 AM
Entity Framework - Reassigning child entity's parent Norm ASP .Net 3 07-06-2009 07:28 PM
How to relate a SQL based entity with an Object based entity in Entity Framework markla ASP .Net 1 10-06-2008 09:42 AM
Entity Name or Entity Number? Samuel van Laere HTML 4 02-24-2007 10:11 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57