简体   繁体   中英

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? can you give any clue about this question?

I will highly appricated your help.

As hr_117 indicates, for all practical purposes the code

<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. 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 .
The <xsl:apply-templates select="."/> inside the for-each will than "trigger" the testsuite template (which is not shown).

Therefore this calls testsuite template (for children testsuite ). This does only do something if there are testsuite nodes inside of testsuites nodes.

Also the for-each is not needed <xsl:apply-templates select="testsuite"/> will do the same.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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