How move figure entity to splited files

Discussion in 'Perl Misc' started by Rahul, May 16, 2007.

  1. Rahul

    Rahul Guest

    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
     
    Rahul, May 16, 2007
    #1
    1. Advertising

  2. Rahul

    mirod Guest

    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
     
    mirod, May 16, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Samuel van Laere

    Entity Name or Entity Number?

    Samuel van Laere, Feb 24, 2007, in forum: HTML
    Replies:
    4
    Views:
    1,655
    Jukka K. Korpela
    Feb 24, 2007
  2. markla
    Replies:
    1
    Views:
    556
    Steven Cheng
    Oct 6, 2008
  3. Norm
    Replies:
    3
    Views:
    2,744
  4. ThatsIT.net.au

    Entity, problem with entity key

    ThatsIT.net.au, Sep 6, 2009, in forum: ASP .Net
    Replies:
    1
    Views:
    1,206
    ThatsIT.net.au
    Sep 7, 2009
  5. Eduardo78
    Replies:
    0
    Views:
    259
    Eduardo78
    Nov 3, 2005
Loading...

Share This Page