Page 1 of 1

How to increment a variable in XSLT

Posted: Thu Nov 20, 2003 8:19 pm
by rprins
Alright, I have the following code:

Code: Select all

<xsl:for-each select="flower">
		<xsl:if test="@color = $Color">
				<tr>
					<td class="flower_info&#123;$number&#125;" style="border-right:1px solid #000000;">
						<xsl:value-of select="name"/>
					</td>
					<td class="flower_info&#123;$number&#125;" style="border-right:1px solid #000000;">
						<xsl:value-of select="id"/>
					</td>
				        <td class="flower_info&#123;$number&#125;" style="border-right:1px solid #000000;">
						<xsl:value-of select="description"/>
					</td>
				</tr>
		</xsl:if>
</xsl:for-each>
And I want to increase the count of the variable number after every loop through. How can I do this?

Posted: Fri Nov 21, 2003 2:39 am
by twigletmac
You can't, variables in XSLT are not like variables in other languages, once they're set they're set and you can't increment them in the way you would do in PHP for instance. However, you can set a variable to something that will change, like position() - as an example:

Code: Select all

<xsl:for-each select="flower">
    <xsl:variable name="number" select="position()" />
    <!-- Bunch more code -->
</xsl:for-each>
Mac

Posted: Fri Nov 21, 2003 3:01 am
by rprins
Yeah, that is how I sued to have it. Something like position() % 2. But, not all of the XML elements are in sequential order, so I don't get even coloring. Hrm, thanks for the relply though. This is kinda what I thought.