Numerical iteration in XSL - Adam Heunis tech talk
in

dotnet.org.za

South African .NET Developer Portal

Adam Heunis tech talk

Software mostly, but other techie stuff as well. I primarily use Notepad, Visual Studio, the Command Prompt, XmlSpy, SQL Server, MySQL and Photoshop. I speak
C# (incl. OO), XML, XSL, ASP.NET, VB.NET, VB6, SQL, UML and XForms.

Numerical iteration in XSL

Have you ever wondered how to iterate a set number of times in XSL? While it is easy to "walk" a nodelist using xsl:for-each or xsl:template, it is a bit more tricky to iterate a set number of times (the "for" statement in C#). Here is what you do:

<?xml version="1.0"?>
<xsl:stylesheet 
version="1.0" xmlns:xsl="...">
<xsl:template 
match="/">
 <xsl:call-template 
name="iterator">
  <xsl:with-param 
name="LastNo">10</xsl:with-param>
 <
/xsl:call-template>
<
/xsl:template>
<xsl:template 
name="iterator">
 <xsl:param 
name="LastNo"/>
 <xsl:param 
name="i" select="0"/>
 <xsl:value-of 
select="$i"/>
 <xsl:if 
test="$i &lt; $LastNo">
  <xsl:call-template 
name="iterator">
   <xsl:with-param 
name="LastNo" select="$LastNo"/>
   <xsl:with-param 
name="i" select="$i + 1"/>
  <
/xsl:call-template>  
 <
/xsl:if>
<
/xsl:template>
<
/xsl:stylesheet>

This will iterate through the "iterator" template 10 (derived from the "LastNo" variable) times.

Published Apr 03 2005, 06:06 AM by attie
Filed under:

Comments

 

TrackBack said:

^_~,pretty good,18showsseeoo!
April 18, 2005 9:39 AM
 

Derrick said:

SWEET! tanks. very helpful.

March 13, 2007 12:55 PM
 

Tony said:

Wonderfulllllll :)

January 15, 2009 4:11 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add
Powered by Community Server (Commercial Edition), by Telligent Systems