Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: errors in printing latex template output

by keszler (Priest)
on Oct 22, 2011 at 12:59 UTC ( [id://933083]=note: print w/replies, xml ) Need Help??


in reply to errors in printing latex template output

Use Data::Dumper or similar to view the structure of your data. The following should get you close to the output you're looking for.

#!/usr/bin/env perl use strict; use warnings;
use XML::Fast; use Template; use Data::Dumper; my $xml = <<'XML'; <student> <number>24</number> <education>bachelors</education> <specialization>computers</specialization> <address> <house_number>128</house_number> <street_name>xxxx</street_name> <proddutoor/> </address> </student> <student> <number>23</number> <education>ph.d.</education> <specialization>physics</specialization> <address> <house_number>12</house_number> <street_name>yyyy</street_name> <kadapa/> </address> </student> XML my $xml_hash = xml2hash $xml; print Dumper($xml_hash),$/; my $template = Template->new(); my $filename = 'output.tex'; $template->process(\*DATA, $xml_hash, $filename) || die "Template process failed: ", $template->error(), "\n"; system( "pdflatex $filename" ); __DATA__ \documentclass{article} \title{Roster} \author{pavani} \begin{document} \maketitle [% FOREACH st IN student %] Student [% st.number %] is a [% st.specialization %] [% st.education % +] student and his address is [% st.address.house_number +%] [% st.add +ress.street_name %]. [% END %] \end{document}

As for reading the XML from file you can just slurp it in:

t.xml =
<student> <number>24</number> <education>bachelors</education> <specialization>computers</specialization> <address> <house_number>128</house_number> <street_name>xxxx</street_name> </address> </student> <student> <number>23</number> <education>ph.d.</education> <specialization>physics</specialization> <address> <house_number>12</house_number> <street_name>yyyy</street_name> </address> </student> <student> <number>22</number> <education>BS</education> <specialization>Biology</specialization> <address> <house_number>123</house_number> <street_name>zzzz</street_name> </address> </student> <student> <number>21</number> <education>MS</education> <specialization>Agronomy</specialization> <address> <house_number>321</house_number> <street_name>aaaa</street_name> </address> </student> <student> <number>20</number> <education>Ph.D</education> <specialization>sociology</specialization> <address> <house_number>333</house_number> <street_name>bbbb</street_name> </address> </student> -- end t.xml #!/usr/bin/env perl use strict; use warnings; use XML::Fast; use Template; use Data::Dumper; my $xmlfile = 't.xml'; my $xml; { open my $xml_fh, '<', $xmlfile or die "cannot open $xmlfile: $!\n"; $/ = ''; $xml = <$xml_fh>; close $xml_fh; } print Dumper($xml),$/; my $xml_hash = xml2hash $xml; print Dumper($xml_hash),$/; my $template = Template->new(); my $filename = 'output.tex'; $template->process(\*DATA, $xml_hash, $filename) || die "Template process failed: ", $template->error(), "\n"; system( "pdflatex $filename" ); __DATA__ \documentclass{article} \title{Roster} \author{pavani} \begin{document} \maketitle [% FOREACH st IN student %] Student [% st.number %] is a [% st.specialization %] [% st.education % +] student and his address is [% st.address.house_number +%] [% st.add +ress.street_name %]. [% END %] \end{document}

Replies are listed 'Best First'.
Re^2: errors in printing latex template output
by veerubiji (Sexton) on Oct 24, 2011 at 08:47 UTC

    Hi, you modified my XML data and after modifying its working in first model. But I has to keep my XML data as original.

    <address> <house_number>12</house_number> <street_name>yyyy</street_name> <kadapa/> </address>
    trying like this its working it printing the data. but
    <address> <house_number="128"/> <street_name="xxxx"/> <kadapa/> </address>
    trying like this its not printing house number and street name. and also I can't understand your code for reading XML data from file. I need to create "t.xml" file separately or I can give my file name directly. because your t.xml file starts with like this
    t.xml=............... .......... --end t.xml
    my file is not start with like t.xml or not end with --end t.xml like that. I am beginner to perl can you tell me.

      The lines t.xml = and -- end t.xml are not in the file t.xml; they were meant to show on screen where the file started and ended.

      I modified your XML data because XML::Fast does not recognize it as XML with a tag and value, or tag, attribute, and value.

      #!/usr/bin/env perl use strict; use warnings; use XML::Fast; use Data::Dumper; my $xml = '<house_number>12</house_number>'; my $xml_hash = xml2hash $xml; print Dumper($xml_hash),$/; $xml = '<house number="12"/>'; $xml_hash = xml2hash $xml; print Dumper($xml_hash),$/; $xml = '<house_number="128"/>'; $xml_hash = xml2hash $xml; print Dumper($xml_hash),$/;
      Output:
      $VAR1 = { 'house_number' => '12' }; $VAR1 = { 'house' => { '-number' => '12' } }; $VAR1 = { 'house_number="128"' => '' };

      If you must use the XMLdata as given, i.e. <house_number="128"/> you'll probably end up having to write your own parser.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://933083]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 05:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found