xsl-list
[Top] [All Lists]

Re: for-loop in xslt problem

2003-04-05 20:54:07
There is an excellent example of a re-usable versatile "for-loop" emulation
template in Orielly's XSLT Def. Guide.

It came in quite handy when i wanted to allow for an integer attribute to
trigger that many copies of something to be output.

Cheers,
Ryan

Quoting Vasu Chakkera <vasucv(_at_)hotmail(_dot_)com>:

Cool. This is interesting.. You can use recursive templates to get what you 
want..
for example fot the xml
<?xml version="1.0"?>
<example>
      <total>45</total>
      <skip>20</skip>
</example>

If you write XSL along the lines of..
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>
      <xsl:template match="/">
              <xsl:call-template name="loop">
                      <xsl:with-param name="maxcount" select="example/total"/>
                      <xsl:with-param name="incriment-factor" 
select="example/skip"/>


                        <xsl:with-param name="initial-value" select="0"/>
              </xsl:call-template>
      </xsl:template>
      <xsl:template name="loop">
              <xsl:param name="maxcount"/>
              <xsl:param name="incriment-factor"/>
              <xsl:param name="initial-value"/>
<!--
This template produces the following logic
for ($count = 0; $count<=$total; ($count = $count + $skip))
{
echo("Print this");
}
-->
              <xsl:if test="$initial-value &lt; $maxcount">
                      <xsl:text>Print this</xsl:text>
                      <xsl:call-template name="loop">
                              <xsl:with-param name="maxcount" 
select="$maxcount"/>
                              <xsl:with-param name="initial-value" 
select="$initial-value+$incriment-factor"/>
                              <xsl:with-param name="incriment-factor" 
select="$incriment-factor"/>
                      </xsl:call-template>
              </xsl:if>
      </xsl:template>
</xsl:stylesheet>

This produces the result:
Print thisPrint thisPrint this

Hope this helps. Please run the XSL against the XML i have given first to 
make sure it works as you wanted. you may then make changes to the xpath to 
reflect your XML.
Please reply back if anything is unclear.
HTH
Vasu


From: Frankie Roberto <public(_at_)frankieroberto(_dot_)com>
Reply-To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
To: "Xsl-List(_at_)Lists(_dot_) Mulberrytech. Com" 
<xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com>
Subject: [xsl] for-loop in xslt problem
Date: Sat, 5 Apr 2003 02:55:43 +0100


Hi,

I'm having problems trying to implement a 'for' loop in xslt - I haven't
seen any syntax so far that can cope with this (and my book doesn't go into
it).

To explain, my XML looks something like:

<total>45</total>
<skip>20</skip>


The PHP script I am converting would do something like this:

for ($count = 0; $count<=$total; ($count = $count + $skip)) {

echo("Print this");

}

..looping $total/$skip times plus once for the remainder.

But I can't see any way of doing this in xslt so far...

Ta.

Frankie


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list



_________________________________________________________________
Hotmail messages direct to your mobile phone http://www.msn.co.uk/mobile


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list





-- 
Ryan Gallagher (binerman)
binerman(_at_)users(_dot_)sourceforge(_dot_)net
The Parchive Project
http://parchive.sourceforge.net
http://sourceforge.net/projects/parchive


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list