Output Number from any level
File: Data.xml <poem> <author>author 1</author> <date>1912</date> <title>Song</title> <stanza> <line>line 1</line> <line>line 2</line> <line>line 3</line> <line>line 4</line> </stanza> <stanza> <line>line 5</line> <line>line 6</line> <line>line 7</line> <line>line 8</line> </stanza> <stanza> <line>line 9</line> <line>line 10</line> <line>line 11</line> <line>line 12</line> </stanza> </poem> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <p> <xsl:apply-templates select="/poem/stanza" /> </p> </body> </html> </xsl:template> <xsl:template match="stanza"> <p> <table> <xsl:apply-templates /> </table> </p> </xsl:template> <xsl:template match="line"> <tr> <td width="350"> <xsl:value-of select="." /> </td> <td width="50"> <xsl:variable name="line-nr"> <xsl:number level="any" from="poem" /> </xsl:variable> <xsl:if test="$line-nr mod 3 = 0"> <xsl:value-of select="$line-nr" /> </xsl:if> </td> </tr> </xsl:template> </xsl:stylesheet> Output: <html> <body> <p> <p> <table> <tr> <td width="350">line 1</td> <td width="50"></td> </tr> <tr> <td width="350">line 2</td> <td width="50"></td> </tr> <tr> <td width="350">line 3</td> <td width="50">3</td> </tr> <tr> <td width="350">line 4</td> <td width="50"></td> </tr> </table> </p> <p> <table> <tr> <td width="350">line 5</td> <td width="50"></td> </tr> <tr> <td width="350">line 6</td> <td width="50">6</td> </tr> <tr> <td width="350">line 7</td> <td width="50"></td> </tr> <tr> <td width="350">line 8</td> <td width="50"></td> </tr> </table> </p> <p> <table> <tr> <td width="350">line 9</td> <td width="50">9</td> </tr> <tr> <td width="350">line 10</td> <td width="50"></td> </tr> <tr> <td width="350">line 11</td> <td width="50"></td> </tr> <tr> <td width="350">line 12</td> <td width="50">12</td> </tr> </table> </p> </p> </body> </html>