Recursion Examples

1. Recursion examples
2. Recursion across two templates

1.

Recursion examples

Jarno Elovirta


> I presently have an XML tag stored as so:
> <rating> 7 </rating>
> It is essentially for rating a product out of a possible score of 10.
> What I want to do is to convert it like this:
> Rating: 1 2 3 4 5 6 7 8 9 10
> where the "7" will be made BOLD.
[c:\temp]type test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.w3.org/1999/xhtml">

<xsl:output method="xml"
            indent="yes"/>

<xsl:variable name="rating" select="normalize-space(rating)" />

<xsl:template match="/">
  <html>
    <head><title></title></head>
    <body>
      <xsl:call-template name="KMFDM">
        <xsl:with-param name="counter" select="1" />
      </xsl:call-template>
    </body>
  </html>
</xsl:template>

<xsl:template name="KMFDM">
  <xsl:param name="counter" />
  <xsl:if test="$counter &lt; 11">
    <xsl:choose>
      <xsl:when test="$counter = $rating">
        <b>
          <xsl:value-of select="$counter" />
        </b>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$counter" />
      </xsl:otherwise>
    </xsl:choose>
    <xsl:text> </xsl:text>
    <xsl:call-template name="KMFDM">
      <xsl:with-param name="counter" select="$counter + 1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

</xsl:stylesheet>

[c:\temp]saxon test.xml test.xsl
<?xml version="1.0" encoding="utf-8" ?>
<html xmlns="http://www.w3.org/1999/xhtml">
   <head>
      <title/>
   </head>
   <body>1 2 3 4 5 6 <b>7</b> 8 9 10 </body>
</html>

Mike Kay adds

<xsl:template name="ratings">
   <xsl:param name="limit" select="10"/>
   <xsl:param name="this" select="1"/>
   <xsl:param name="emph"/>
   <xsl:choose>
   <xsl:when test="$this=$emph"><b>&#xa0; <xsl:value-of
select="$this"/></b></xsl:when>
   <xsl:otherwise>>&#xa0; <xsl:value-of select="$this"/></xsl:otherwise>
   </xsl:choose>
   <xsl:if test="$this &lt; $limit">
         <xsl:call-template name="ratings">
             <xsl:with-param name="limit" select="$limit"/>
             <xsl:with-param name="this" select="$this+1"/>
             <xsl:with-param name="emph" select="$emph"/>
         </xsl:call-template>
    </xsl:if>
</xsl:template>

2.

Recursion across two templates

Jeni Tennison

I'm experimenting with XSLT and SVG: http://www.pinkjuice.com/SVG/XSLT/fractals/ (those are working)

Now I want to generate different colors for the levels; forth and back in an array of colors. The level can be set to any depth, and I don't want to just go from blue to yellow for a result with 2 levels as well as one with 10 levels; I'd rather have the colors go from blue to yellow to blue to yellow and so forth for the depth set in the input-doc ("up and down"). (the opacity of the levels goes one way from low to high for any depth; this works.)

> I tried to write two recursing templates: the first is called
> "forth"; red and blue are incremented, and green is decremented. The
> second one works the otherway round. One should start the other as
> soon as one of the values reached the limit of either 0 or 255.
> Both should stop as soon as the set level of depth is reached.

The important thing is that last one. "Both should stop as soon as the set level of depth is reached." Looking at the templates, you test $depth in both, but in both it's in an xsl:when and there's an xsl:otherwise which catches the other option. So for example:

<xsl:template name="back">
   ...
   <xsl:choose>
      <xsl:when test="$current &lt; $depth and ($red &gt; 0)
                      and ($green &lt; 255) and ($blue &gt; 0)">
         <xsl:call-template name="back">
            ...
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:call-template name="forth">
            ...
         </xsl:call-template>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>

You need to *not do anything* when $current &gt;= $depth, rather than just going into xsl:otherwise. So something like:

<xsl:template name="back">
   ...
   <xsl:choose>
      <xsl:when test="$current &gt;= $depth" />
      <xsl:when test="($red &gt; 0) and ($green &lt; 255)
                      and ($blue &gt; 0)">
         <xsl:call-template name="back">
            ...
         </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
         <xsl:call-template name="forth">
            ...
         </xsl:call-template>
      </xsl:otherwise>
   </xsl:choose>
</xsl:template>