How to increment a variable in XSLT

XML, Perl, Python, and other languages can be discussed here, even if it isn't PHP (We might forgive you).

Moderator: General Moderators

Post Reply
User avatar
rprins
Forum Commoner
Posts: 31
Joined: Sat Jun 21, 2003 5:46 pm

How to increment a variable in XSLT

Post 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?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
User avatar
rprins
Forum Commoner
Posts: 31
Joined: Sat Jun 21, 2003 5:46 pm

Post 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.
Post Reply