Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

errors in printing latex template output

by veerubiji (Sexton)
on Oct 22, 2011 at 11:40 UTC ( #933058=perlquestion: print w/ replies, xml ) Need Help??
veerubiji has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I generated latex template using perl,and complied with MikTeX, its genarating pdf.But I have some problems. I written script like this

#!/usr/bin/env perl use strict; use warnings; use XML::Fast; use Template; my $xml = <<'XML'; <student> <number>24</number> <education>bachelors</education> <specialization>computers </specialization> <address> <house_number="128"/> <street name="xxxx"/> <proddutoor/> </address> </student> <student> <number>23</number> <education>ph.d.</education> <specialization>physics </specialization> <address> <house_number="12"/> <street name="xxxx"/> <kadapa/> </address> </student> XML my $xml_hash = xml2hash $xml; 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.degree %] s +tudent and his address is[%address%][%house_number%][%street name%]. [% END %] \end{document}
when I run it its generating pdf with like this
student 24 is a computers student and his address is HASH(0x274b27c) student 23 is a computers student and his address is HASH(0x274b27c)
but I need to print address also how to get the address element content also like this
student 24 is a computers student and his address is house_number="128"; street name="xxxx"; proddutoor. student 23 is a computers student and his address is house_number="12"; street name="xxxx"; kadapa.
what should I need to change to print like this in pdf. 2)my second question is in my code i assigned only two students information to the variable $xml. But i have 150 students information in one file how can i print all students information in pdf. in this code i am printing only two students information. like this same format i have more number of stdents information in one file, I opened that file and assigned like this
my $file; open($file, 'formal.xml'); my $xml=$file; my $xml_hash = xml2hash $xml;
but its not working when i given a file as input.

Comment on errors in printing latex template output
Select or Download Code
Re: errors in printing latex template output
by jethro (Monsignor) on Oct 22, 2011 at 12:40 UTC

    For your template code to work I suspect something like [%st.address.house_number%] could work (untested).

    I assume with this line "my $xml=$file;" you somehow want to read in the file contents into $xml. Not gonna happen ;-). For reading from files usually the operator "<>" is used. Normally just like this: "my @xml=<$file>;" But since the documentation of XML::Fast doesn't say anything about xml2hash parameters, it is to be feared that arrays as input don't work. So you need this:

    open($file, 'formal.xml'); { local $/; my $xml= <$file>; }

    Similar code is easily found by googling for "perl read file into scalar". The important thing is the undefining of $/, the input record separator (see perlvar). Because of that line endings are not recognized when reading the file and the file contents is returned as one single variable instead of an array of lines.

      hi, I tried as you Said like this

      open($file, 'formal.xml'); { local $/; my $xml= <$file>; }
      it given two errors like $xml and $file requires explicit package and I cleared those errors like this
      my $file; my $xml; open($file, 'formal.xml'); { local $/; $xml= <$file>; }
      but it doesn't produce any output its just blank pdf with author name. I tried using data dumper to print $xml_hash. but it doesn't contain any student word it number and regarding information in hash.then I think how it will recognize student. and also I tried to print address elements as you said
      [%st.address.house_number%]
      its also not working. its also printing as before I posted.

        Sorry, my fault. The "my" in "my $xml= <$file>;" is wrong and should be removed. Your correction should work as well. Did you notice that you use "DTC_Specification_transformed.xml" as filename instead of "formal.xml" in your corrected script?

        Use data::dumper to print out $xml as well just before converting it with xml2hash. If it contains the file contents then at least up to that point the script is working

Re: errors in printing latex template output
by keszler (Priest) on Oct 22, 2011 at 12:59 UTC

    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;

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

    t.xml =

      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
Node Status?
node history
Node Type: perlquestion [id://933058]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2014-09-03 06:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (35 votes), past polls