RE: [xsl] Recursive for loop & xslt 2.0

Subject: RE: [xsl] Recursive for loop & xslt 2.0
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Wed, 17 Jun 2009 13:31:49 +0100
You are passing the parameters as strings rather than as numbers, so in XSLT
2.0 they will be compared as strings.

Instead of

  <xsl:with-param name="i">1</xsl:with-param>

write

  <xsl:with-param name="i" select="1"/>

and instead of

  <xsl:with-param name="count">
    <xsl:value-of select="$count"/>
  </xsl:with-param>

write

<xsl:with-param name="count" select="$count"/>

(This habit of using xsl:value-of inside xsl:variable or xsl:with-param,
rather than using the select attribute, is really bad XSLT coding, and yet
it seems to be so widely practiced it's impossible to eradicate. It's
long-winded, it's grossly inefficient, and in some cases, as here, it gives
the wrong answer.)

Regards,

Michael Kay
http://www.saxonica.com/
http://twitter.com/michaelhkay 


 

> -----Original Message-----
> From: Israel Viente [mailto:israel.viente@xxxxxxxxx] 
> Sent: 17 June 2009 13:07
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Recursive for loop & xslt 2.0
> 
> Hi,
> 
> I used an example of a recursive for loop as below:
> <?xml version="1.0" encoding="utf-8"?>
> <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_: Send_Loop_To_HTML -->
>     <xsl:call-template name="for.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="for.loop">
> 
>    <xsl:param name="i"      />
>    <xsl:param name="count"  />
> 
>    <!--begin_: Line_by_Line_Output -->
>    <xsl:if test="$i &lt;= $count">
>       <br /> <b><xsl:value-of select="$i" />.</b>Hello world!
>    </xsl:if>
> 
>    <!--begin_: RepeatTheLoopUntilFinished-->
>    <xsl:if test="$i &lt;= $count">
>       <xsl:call-template name="for.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>
> 
> As soon as I change the xsl:stylesheet/@version to "2.0" it 
> stops working as expected.
> 
> Can you please let me know what am I missing?
> 
> Thanks, Viente

Current Thread