Since you aren't supposed to be able to increment a counter inside a for-each loop...
<xsl:variable name="elemCount" select="0"/>
<xsl:for-each select="elem">
<xsl:variable name="elemCount" select="$elemCount+1"/>
</xsl:for-each>
I have resorted to tricks like...
<xsl:variable name="temp">
<xsl:for-each select="elem">
<xsl:text>1</xsl:text>
</xsl:for-each>
</xsl:variable>
<xsl:variable name="elemCount" select="string-length($temp)"/>
Of course there are also many circumstances where the XPATH node set functions last(), position() and count() provide the integers you need to control your loops.