English 中文(简体)
如何利用模板工具包获取这些要素
原标题:how to get this elements using template toolkit

Hi

<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>

我使用了XML:Simple和I生成了散射结构数据并储存在一个变量中。 我使用了模板:toolkit生成pdffile(使用pdflatex)。 我的XML:简单产出就是这样。

$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(), "
";
  system( "pdflatex $filename" );
  __DATA__
 documentclass[a4paper,leqno,twoside]{article}
  egin{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.

最佳回答

我认为,你刚才需要关于这些章节的另外一个<条码>FOREACH,例如:

[%+ 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 %]

http://template-toolkit.org/docs/manual/Filters.html 如果您不注意如何处理新线路,则使用<代码>[%......%],。 ......+%] 版本保存着铅和拖车的白色空间。

问题回答

你过去的情况似乎并不完全正确。 你们是否 j笑:

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 SOON) %]

你们应避免结构太深。





相关问题
Why does my chdir to a filehandle not work in Perl?

When I try a "chdir" with a filehandle as argument, "chdir" returns 0 and a pwd returns still the same directory. Should that be so? I tried this, because in the documentation to chdir I found: "...

How do I use GetOptions to get the default argument?

I ve read the doc for GetOptions but I can t seem to find what I need... (maybe I am blind) What I want to do is to parse command line like this myperlscript.pl -mode [sth] [inputfile] I can use ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签