pavani
pavani

Reputation: 143

how to get this elements using template toolkit

Hi I have xml data like this

<university>
     <name>svu</name>
     <location>ravru</location>
      <branch>
           <electronics>
                  <Section>
                      <student name="xxx" number="12">
                           <sem semister="1"subjects="7" rank="1"/>
                           <sem semister="2"subjects="4" rank="1"/>
                      <student>
                      <student name="xxx" number="15">
                           <sem semister="1"subjects="7" rank="10"/>
                           <sem semister="2"subjects="4" rank="1"/>
                      <student>
                      <student name="xxx" number="16">
                           <sem semister="1"subjects="7" rank="2"/>
                           <sem semister="2"subjects="4" rank="2"/>
                      <student>
                 </section>
            </electronics>
         </branch>
 </university>
 <university>
     <name>sku</name>
     <location>ANTP</location>
      <branch>
           <computers>
                  <Section>
                      <student name="xxx" number="12">
                           <sem semister="1"subjects="7" rank="no"/>
                           <sem semister="2"subjects="4" rank="no"/>
                      <student>
                      <student name="xxx" number="15">
                           <sem semister="1"subjects="7" rank="10"/>
                           <sem semister="2"subjects="4" rank="1"/>
                      <student>
                      <student name="xxx" number="16">
                           <sem semister="1"subjects="7" rank="20"/>
                           <sem semister="2"subjects="4" rank="21"/>
                      <student>
                 </section>
            </electronics>
         </branch>
 </university>

I used XML::Simple and I generated hash structured data and stored in a variable. I used template::toolkit to generate pdffile(using pdflatex).my XML::simple output is like this

$var1={
     university=>{
          'name'=>'svu',
           'location'=>'ravru',
            'branch'=>{
                     'electronics'=>{
                                'section'=>[
                                         {
                                        'name'=>'xxx',
                                         'number'=>'12',
                                           'sem'=>[
                                                {
                                               'semister'=>'1',
                                                'subjects'=>'7',
                                                 'rank'=>'1'
                                                  },
                                                 {
                                                'semister'=>'2',
                                                'subjects'=>'4',
                                                 'rank'=>'1'
                                                  }
                                                 ]
                                               },
                                             {
                                        'name'=>'xxx',
                                         'number'=>'15',
                                           'sem'=>[
                                                {
                                               'semister'=>'1',
                                                'subjects'=>'7',
                                                 'rank'=>'10'
                                                  },
                                                 {
                                                'semister'=>'1',
                                                'subjects'=>'7',
                                                 'rank'=>'1'
                                                  }
                                                 ]
                                               },
                                              {
                                        'name'=>'xxx',
                                         'number'=>'16',
                                           'sem'=>[
                                                {
                                               'semister'=>'1',
                                                'subjects'=>'7',
                                                 'rank'=>'2'
                                                  },
                                                 {
                                                'semister'=>'2',
                                                'subjects'=>'4',
                                                 'rank'=>'2'
                                                  }
                                                 ]
                                               }
                                             }
                                          ]
                                         }
                                       };

like this I have 15 universities and also some section having only two or three students some having 10 students, in students some body only one sem some having two semesters like that.I written template like this

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 = university %]
   [%+ st.name +%]
   [%+ st.location +%]
 [% FOREACH section = st.branch.electronics.section %]
  branch student: [%+ section.name +%]
 [%+ section.number +%]
 [% FOREACH sem = section.sem %]
    [%+ sem.semister +%]
    [%+ sem.subjects +%]
    [%+ sem.rank +%]
[% END %]
 [% END %]
   [% END %]
  /end{document}

like this it giving output but My problem is its too lengthy and also I said above some section have two students and some sections have more than two. for example first section contains 1 student and second section contains 10 students like that. but in pdf first section also printing 10 students only first student have information remaining all empty. how can I eliminate this problem. If you dont understand my problem just how can I get the data using shorter code than i written. is there any other way to get branch elements all with simpler using for loop for branch elements or any simple code.because if some section have 60 students so it printing 6o in every section.

Upvotes: 1

Views: 253

Answers (2)

mu is too short
mu is too short

Reputation: 434665

I think you just need another FOREACH on the sections, something like this:

[%+ university.name +%]
[%+ university.location +%]
[% FOREACH section = university.branch.electronics.section %]
    branch student: [%+ section.name +%]
    [%+ section.number +%]
    [% FOREACH sem = section.sem %]
        [%+ sem.semister +%]
        [%+ sem.subjects +%]
        [%+ sem.rank +%]
    [% END %]
[% END %]

You might want to add a custom filter, custom vmethod, or plugin to make sure all your strings are properly escaped for use in LaTeX. If you don't care about how newlines are treated then use [% ... %], the [%+ ... +%] version preserves leading and trailing whitespace.

Upvotes: 1

Беров
Беров

Reputation: 1393

What you have pasted above does not seems syntactically correct. Are you jocking :) ?

Anyway, read the Introduction of the manual.... After passing your structure to the TT stash you can acces its elements for example like:

[% var1.STC.0.gym.hyd.com (AND SO ON) %]

You should avoid too deep structures.

Upvotes: 1

Related Questions