help counter recursive template

Discussion in 'XML' started by Petterson Mikael, Feb 9, 2006.

  1. Hi,

    I call the sequenceNameString template with:

    <xsl:when test="child::*[name() = 'sequence']">
    <xsl:call-template name="sequenceNameString">
    <xsl:with-param name="sequenceName" select="@name"/>
    <xsl:with-param name="maxCount" select="sequence/maxLength"/>
    <xsl:with-param name="value" select="0"/>
    </xsl:call-template></xsl:when>

    I can check that the parameter values are sent to the template.
    However the incrementation of value does not happen.

    I get the following only once( example):

    eAgchCodes_name0

    but want:

    eAgchCodes_name0
    eAgchCodes_name1
    eAgchCodes_name2
    .......

    Any ideas why?

    cheers,

    //Mikael

    !-- Template that create "<sequencename>_name0", "<sequencename>_name1"..-->
    <xsl:template name="sequenceNameString">
    <xsl:param name="sequenceName"/>
    <xsl:param name="maxCount"/>
    <xsl:param name="value"/>
    <xsl:param name="arrayMax" select="$maxCount - 1"/>
    <xsl:if test="$value &lt; $arrayMax">
    <xsl:value-of
    select="concat(concat($sequenceName,'_name'),$value)"/>
    <xsl:call-template name="sequenceNameString">
    <xsl:with-param name="value" select="$value + 1"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>
     
    Petterson Mikael, Feb 9, 2006
    #1
    1. Advertising

  2. Haven't examined it in detail, but I note one glitch: You aren't passing
    maxCount down through the recursive call.
     
    Joe Kesselman, Feb 10, 2006
    #2
    1. Advertising

  3. Yep, that was it. (You're also not outputting line breaks between the
    generated names, but I'll leave that as an exercise for the student.)

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template match="/">
    <xsl:call-template name="sequenceNameString">
    <xsl:with-param name="sequenceName" select="'SN'"/>
    <xsl:with-param name="maxCount" select="4"/>
    <xsl:with-param name="value" select="0"/>
    </xsl:call-template>
    </xsl:template>

    <xsl:template name="sequenceNameString">
    <xsl:param name="sequenceName"/>
    <xsl:param name="maxCount"/>
    <xsl:param name="value"/>
    <!-- You used xsl:param for arrayMax; should have been variable. -->
    <xsl:variable name="arrayMax" select="$maxCount - 1"/>
    <xsl:if test="$value &lt; $arrayMax">
    <xsl:value-of
    select="concat(concat($sequenceName,'_name'),$value)"/>
    <xsl:call-template name="sequenceNameString">
    <!-- The real bug was that you forgot to pass both
    sequenceName and maxCount. -->
    <xsl:with-param name="sequenceName" select="$sequenceName"/>
    <xsl:with-param name="value" select="$value + 1"/>
    <xsl:with-param name="maxCount" select="$maxCount"/>
    </xsl:call-template>
    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>
     
    Joe Kesselman, Feb 11, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sam  Collett

    Recursive counter

    Sam Collett, Nov 3, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    413
    Marina
    Nov 3, 2004
  2. Sam  Collett

    Recursive counter

    Sam Collett, Nov 3, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    524
    Sam Collett
    Nov 4, 2004
  3. The Eeediot
    Replies:
    3
    Views:
    2,262
    =?Utf-8?B?UnVsaW4gSG9uZw==?=
    Dec 22, 2004
  4. George2
    Replies:
    1
    Views:
    820
    Alf P. Steinbach
    Jan 31, 2008
  5. Thibaut Barrère

    Recursive line counter (review?)

    Thibaut Barrère, Mar 13, 2006, in forum: Ruby
    Replies:
    3
    Views:
    94
    Robert Klemme
    Mar 13, 2006
Loading...

Share This Page