texuser74 has asked for the wisdom of the Perl Monks concerning the following question:

Dear Perl Experts,

I have an input file "xxx.in" with the data

<item><name>Donald</name><title>I Title</title></item>
<item><name>James</name><title>II Title</title></item>

and a template "template.txt" as

<item><name></name><title></title><year></year></item>

i want to generate the output file "xxx.out" by filling the template from by picking the input from "xxx.in".

my output "xxx.out" file should look like

<item><name>Donald</name><title>I Title</title><year></year></item>
<item><name>James</name><title>II Title</title><year></year></item>

please help in doing this.

Replies are listed 'Best First'.
Re: Find and Replace using Multiple files
by PodMaster (Abbot) on Aug 30, 2004 at 12:25 UTC
    I don't know what kind of template that is, but for that particular example I'd do
    #!/usr/bin/perl -w -- use XML::Twig; my $twig = XML::Twig->new( twig_roots => { 'item' => sub { XML::Twig::Elt->new( year => '', )->paste( last_child => $_[-1], ); $_[-1]->print; print $/; } }, ); my $xml =<<XML; <item><name>Donald</name><title>I Title</title></item> <item><name>James</name><title>II Title</title></item> XML $twig->parse('<junk>'.$xml.'</junk>'); # junk cause $xml ain't really +xml
    I'd use Template for templating. I'd also use XSLT for xml transformation, or XML::Twig when XSLT isn't suitable.

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Find and Replace using Multiple files
by pelagic (Priest) on Aug 30, 2004 at 12:08 UTC
    You obviously want to do a transformation from XML to XML.
    You might find some interesting links about XSLT here.

    pelagic