Loop with XSL..
The kind of language that XSL is, with nothing written straight forward and a real nightmare for once having a first glance, it has no option for looping..There is theSearching around in the net for this was no joke either, for very few got me where i wanted. And finally i got one link that seemed god sent..posted just for me..!
Posting the code here:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"/>
<xsl:template match="/">
<html>
<head><title>Say Hello Ten Times!</title></head>
<body>
<b>I am going to say hello Ten Times!</b>
<!-- begin_: Call the loop function -->
<xsl:call-template name="loop">
<xsl:with-param name="i">1</xsl:with-param>
<xsl:with-param name="count">10</xsl:with-param>
</xsl:call-template>
</body>
</html>
</xsl:template>
<!--begin_: Define_The_Output_Loop -->
<xsl:template name="loop">
<xsl:param name="i" />
<xsl:param name="count" />
<!--output to be printed for every loop-->
<xsl:if test="$i <= $count">
<br /><b><xsl:value-of select="$i" />.</b>Hello World
</xsl:if>
<!--Repeat The Loop Until Finished-->
<xsl:if test="$i <= $count">
<xsl:call-template name="loop">
<xsl:with-param name="i">
<xsl:value-of select="$i + 1"/>
</xsl:with-param>
<xsl:with-param name="count">
<xsl:value-of select="$count"/>
</xsl:with-param>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
------------------------------------------------------------------------
learnings from this venture:
1. This is a huge workaround for a simple operation done by a for loop. Wonder why w3c dint come up with something simpler for it.
2. No new template can be defined within another. So make the call in place, and define the template elsewhere.
3. It doesnt matter whether u define the template before or after the call for template.
4. Templates rock.!

0 Comments:
Post a Comment
<< Home