I am processing an XML file where I want to keep count of the number of nodes, so that I can use it as an ID as I write new nodes.
At the moment I have a global vari
Use <xsl:variable name="RowNum" select="count(./preceding-sibling::*)" />
and $RowNum as an incrementing value.
Eg: <xsl:template name="ME-homeTiles" match="Row[@Style='ME-homeTiles']" mode="itemstyle">
<xsl:variable name="RowNum" select="count(./preceding-sibling::*)" />
...<a href="{$SafeLinkUrl}" class="tile{$RowNum}"><img ....></a>
This will create classes for link with values tile1, tile2, tile3 etc...
Haven't tried this myself, but you could try and pass a parameter to the template. In your first template you set the parameter to count() (or current() maybe?) within the for-each statement and then pass that value to your "section" template.
Here's more on passing parameters to templates
XSLT variables cannot be changed. You'll have pass the value along from template to template.
If you are using XSLT 2.0, you can have parameters and use tunneling to propagate the variable to the right templates.
Your template will look something like this:
<xsl:template match="a">
<xsl:param name="count" select="0">
<xsl:apply-templates>
<xsl:with-param select="$count+1"/>
</xsl:apply-templates>
</xsl:template>
Also look at using generate-id() if you want to create ids.
variables are locally scoped and read only in xslt.
Depending on your XSLT processor, you may be able to introduce scripted functions into your XLST. For example, the Microsoft XML library supports the inclusion of javascript. See http://msdn.microsoft.com/en-us/library/aa970889(VS.85).aspx for an example. This tactic obviously won't work if you're planning to deploy/execute XSLT on public client browsers; it has to be done by a specific XSLT processor.
Variables in XSLT are immutable so you have to approact the problem with that in mind. You could either use position()
directly:
<xsl:template match="/">
<xsl:for-each select="section">
<xsl:call-template name="section"/>
</xsl:for-each>
</xsl:template>
<xsl:template name="section">
<span class="title" id="title-{position()}"><xsl:value-of select="title"/></span>
</xsl:template>
Or in a more template orientated way:
<xsl:template match="/">
<xsl:apply-templates select="section"/>
</xsl:template>
<xsl:template match="section">
<span class="title" id="title-{position()}"><xsl:value-of select="title"/></span>
</xsl:template>