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

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Hi, I have XML file with more number of university's like this as shown below

<university> <name>svu</name> <location>ravru</location> <branch> <electronics> <student name="xxx" number="12"> <semester number="1"subjects="7" rank="2"/> </student> <student name="xxx" number="15"> <semester number="1" subjects="7" rank="10"/> <semester number="2" subjects="4" rank="1"/> </student> <student name="xxx" number="16"> <semester number="1"subjects="7" rank="2"/> <semester number="2"subjects="4" rank="2"/> </student> </electronics> </branch> </university> <university> <name>sku</name> <location>ANTP</location> <branch> <electronics> <student name="xxx" number="12"> <semester number="3"subjects="6" rank="20"/> </student> <student name="xxx" number="16"> <semester number="1"subjects="9" rank="12"/> <semester number="2"subjects="4" rank="2"/> </student> </section> </electronics> </branch> </university>

I used XML::Simple to store this data into one hash data structure variable $data and that information is like this

$var1={ university=>{ 'name'=>'svu', 'location'=>'ravru', 'branch'=>{ 'electronics'=>{ 'student'=>[ { 'name'=>'xxx', 'number'=>'12', 'semester'=>{ 'Number'=>'1', 'subjects'=>'7', 'rank'=>'1' } }, { 'name'=>'xxx', 'number'=>'15', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'10' }, { 'number'=>'1', 'subjects'=>'7', 'rank'=>'1' } ] }, { 'name'=>'xxx', 'number'=>'16', 'semester'=>[ { 'number'=>'1', 'subjects'=>'7', 'rank'=>'2' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'2' } ] } } ] } }; university=>{ 'name'=>'sku', 'location'=>'ANTP', 'branch'=>{ 'electronics'=>{ 'student'=>[ { 'name'=>'xxx', 'number'=>'12', 'semester'=>{ 'Number'=>'3', 'subjects'=>'6 +', 'rank'=>'20' } }, { 'name'=>'xxx', 'number'=>'6', 'semester'=>[ { 'number'=>'1', 'subjects'=>'9', 'rank'=>'12' }, { 'number'=>'2', 'subjects'=>'4', 'rank'=>'2' } ] } } ] } };

by using template tool kit and pdflatex compiler I am generating PDF file , I written like this as shown below.

my $template = Template->new(); my $filename = 'output.tex'; $template->process(\*DATA, $data, $filename) || die "Template process failed: ", $template->error(), "\n"; system( "pdflatex $filename" ); __DATA__ \documentclass[a4paper,leqno,twoside]{article} \begin{document} [% FOREACH st IN university %] [% st.name %] [%st.location%] studentdata: [% FOREACH student IN st.branch.electronics.student %] Component type: [%+ student.name +%][%+ student.number +%] [% FOREACH semester IN student.semester %] [%+ semester.number +%] [%+ semester.subjects +%] [%+ semester.rank +%] [% END %] [% END %] [% END %]
\end{document}

But I getting Different output rather than what I want. After executing this script I am getting output when I have three or more students in electronics and two or more semesters for each student then only I am getting. otherwise it does print any thing just printing studentdata: three times . for example if I have one student with one semester in university at that situation it doesn't give any thing it printing like this studentdata: studentdata: studentdata: like as above ,I identified the problem is I have three attributes in student element "name""number" and "semester" like this so it printing three times If I add one more attribute in between name and number in student element it printing four times. It means something wrong with second for loop but I cant rectify it. I tried alot . how can I rectify and how can I print data like this in pdf.

name: svu location: ravru student data: student name=xxx number=12 semester number=1 subjects=7 rank=2 student name=xxx number=15 semester number=1 subjects=7 rank=10 semester number=2 subjects=4 rank=1 student name=xxx number=16 semester number=1 subjects=7 rank=2 semester number=2 subjects=4 rank=2

I want to print like this can any body help me.


In reply to problem with hash iteration in template by veerubiji

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2024-03-29 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found