<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<!-- This doesn't work
<xsl:variable name="COUNT" select="1"/>

<xsl:template match="foo">
	<barcount>
		<xsl:for-each select="bar">
			<count>
				<xsl:value-of select="$COUNT"/>
			</count>
			<xsl:variable name="COUNT" select="$COUNT + 1"/>
		</xsl:for-each>
	</barcount>
</xsl:template>
-->

<!-- Use recursion -->
<xsl:template match="foo">
	<barcount>
		<xsl:apply-templates select="bar[1]"/>
	</barcount>
</xsl:template>

<xsl:template match="bar">
	<xsl:param name="CURRENT" select="1"/>
	<count>
		<xsl:value-of select="$CURRENT"/>
	</count>
	<xsl:apply-templates select="following-sibling::bar[1]">
		<xsl:with-param name="CURRENT" select="$CURRENT + 1"/>
	</xsl:apply-templates>
</xsl:template>

</xsl:stylesheet>