0

I have this Following XSLT snippet:

    <table border="1" id ="test">
    <xsl:for-each select= "TestSuite/TestCase">

                <tr>
                <xsl:attribute name="id">
                        <xsl:value-of select="count(preceding-sibling::*)"/>
                </xsl:attribute>
                <b><xsl:value-of select="@name"/></b>

                </tr>

                <xsl:for-each select="Verification|Command">
                        <tr>
                        <xsl:attribute name="id">
                            <xsl:value-of select="count(preceding-sibling::*)"/>
                        </xsl:attribute>
                        <xsl:choose>
                                <xsl:when test="contains(name() , 'Verification')">

                                <td>Verification <xsl:value-of select="@type"/></td>
                                <td><xsl:value-of select="@status"/></td>
                        </xsl:when>
                        <xsl:when  test="contains(name() , 'Command')">
                                <td>Command <xsl:value-of select="@type"/></td>
                                <td><xsl:value-of select="@status"/></td>
                        </xsl:when>
                        </xsl:choose>

                        </tr>


                </xsl:for-each>

      </xsl:for-each>
      </table>  

Now I would simply like to give each table row an id starting with 0, 1 then 2 etc. The problem is that every inner loop starts the id counting by 0 again. How can I solve this? My HTML Page shows only one table so all tr should be siblings.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Hans En
  • 906
  • 5
  • 15
  • 32
  • Is the use of consecutive integers a requirement? Otherwise you could just concatenate the id of the outer loop with those of the inner one ('0-0', '0-1', '0-2', '1-0', '1-1' etc.) – MiMo May 16 '13 at 15:47
  • Yes because i need to scroll to the elements using the id. Because I don´t know what the XML looks like beforehand it would be nice if they were consecutive. – Hans En May 16 '13 at 15:50

2 Answers2

0

How about something like this:

  <xsl:template match="something">
    <table border="1" id ="test">
      <xsl:apply-templates select="TestSuite/TestCase | 
                                   TestSuite/TestCase/*[self::Verification or
                                                        self::Command]" />
    </table>
  </xsl:template>

  <xsl:template match="TestSuite/TestCase">
    <tr id="{position()}">
      <td colspan="2">
        <b>
          <xsl:value-of select="@name"/>
        </b>
      </td>
    </tr>
  </xsl:template>

  <xsl:template match="TestCase/Verification | TestCase/Command">
    <tr id="{position()}">
      <td>
        <xsl:value-of select="concat(local-name(), @type)"/>
      </td>
      <td>
        <xsl:value-of select="@status"/>
      </td>
    </tr>
  </xsl:template>
JLRishe
  • 99,490
  • 19
  • 131
  • 169
0

Instead of basing your id on the position, just use generate-id():

id="{generate-id()}"

This will not only be unique, but will also be a valid ID type value.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95