如何使用模板工具包获取此元素

发布于 2024-12-12 12:25:02 字数 6413 浏览 0 评论 0原文

您好,我有像这样的 xml 数据

<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 生成哈希结构化数据并存储在变量中。我使用 template::toolkit 生成 pdffile(使用 pdflatex)。我的 XML::simple 输出是这样的

$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'
                                                  }
                                                 ]
                                               }
                                             }
                                          ]
                                         }
                                       };

我有 15 所大学,有些部分只有两到三名学生,有些有 10 名学生,有些学生只有一名SEM有些有两个学期这样。我写了这样的模板,

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}

这样它给出了输出,但我的问题是它太长了,而且我上面说过有些部分有两个学生,有些部分有两个以上的学生。例如,第一部分包含 1 名学生,第二部分包含 10 名学生,这样。但在 pdf 第一节中也打印了 10 个学生,只有第一个学生的信息全部为空。我怎样才能消除这个问题。 如果您不理解我的问题,我如何使用比我编写的更短的代码获取数据。有没有其他方法可以更简单地使用分支元素的 for 循环或任何简单的代码来获取所有分支元素。因为如果某个部分有 60 名学生,那么它会在每个部分打印 6o。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

§普罗旺斯的薰衣草 2024-12-19 12:25:02

我认为您只需要在这些部分上另一个 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 %]

您可能需要添加 自定义过滤器, 自定义 vmethod,或 插件 确保所有字符串都正确转义以便在 LaTeX 中使用。如果您不关心如何处理换行符,请使用 [% ... %][%+ ... +%] 版本保留前导和尾随空白。

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.

幸福%小乖 2024-12-19 12:25:02

您上面粘贴的内容在语法上似乎不正确。你在开玩笑吗:)?

无论如何,请阅读手册简介......
将结构传递到 TT 存储后,您可以访问其元素,例如:

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

您应该避免太深的结构。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文