简体   繁体   English

如何理解这个xsl模板选择

[英]how to understand this xsl template select

how to understand this code block, 如何理解这个代码块,

<xsl:for-each select="testsuite">
  <xsl:apply-templates select="."/>
</xsl:for-each>

using in below, 在下面使用,

<xsl:template match="testsuites">
    <table border="1" width="100%">
    <tr bgcolor="#9acd32">
      <th align="left">module</th>
      <th align="left">tests</th>
      <th align="left">time</th>
    </tr>
    <tr>
      <td><xsl:value-of select="@name"/></td>
      <td><xsl:value-of select="@tests"/></td>
      <td><xsl:value-of select="@time"/></td>
    </tr>
    </table>
    <br/>

    <xsl:for-each select="testsuite">
      <xsl:apply-templates select="."/>
    </xsl:for-each>
</xsl:template>

what's template the <xsl:apply-templates/> apply, according above code? 根据上面的代码, <xsl:apply-templates/>适用的模板是什么? can you give any clue about this question? 你能对这个问题提出任何线索吗?

I will highly appricated your help. 我会高度评价你的帮助。

As hr_117 indicates, for all practical purposes the code 正如hr_117所示,出于所有实际目的,代码

<xsl:for-each select="testsuite">
  <xsl:apply-templates select="."/>
</xsl:for-each>

is equivalent to 相当于

<xsl:apply-templates select="testsuite"/>

It's actually not 100% equivalent, so exercise slight caution before rewriting it: in the first case, a call on position() within the selected template will always return 1, while in the second case it will return the position of the testsuite within the set of sibling testsuite elements. 它实际上并非100%等效,所以在重写之前要小心谨慎:在第一种情况下,对所选模板中的position()的调用将始终返回1,而在第二种情况下,它将返回testuite中的位置。一套兄弟测试元素。 That's very unlikely to matter, however. 然而,这不太重要。

The <xsl:for-each select="testsuite"> statement iterate over all children of the current node (which is testsuites . <xsl:for-each select="testsuite">语句遍历当前节点的所有子节点(即testsuites
The <xsl:apply-templates select="."/> inside the for-each will than "trigger" the testsuite template (which is not shown). for-each<xsl:apply-templates select="."/>比“触发” testsuite模板(未显示)。

Therefore this calls testsuite template (for children testsuite ). 因此,这称为testsuite模板(用于儿童testsuite )。 This does only do something if there are testsuite nodes inside of testsuites nodes. 这不只是做一些事情,如果有testsuite的内部节点testsuites的节点。

Also the for-each is not needed <xsl:apply-templates select="testsuite"/> will do the same. 此外,不需要for-each <xsl:apply-templates select="testsuite"/>也会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM