xsl-list
[Top] [All Lists]

[xsl] Recursively looping through a template only X number of times?

2006-11-28 10:05:11
I'm building a table of contents from my XML file. However, I only want
to display the nodes x levels deep. For now, it's 2, but perhaps we
might want to change that later. As such, I'm trying to write the XSL so
we can just change a variable or xsl:if test to change the depth of the
TOC.

Here's a sample set of templates I am using:

The first template starts the parent UL and grabs each of the parent
level nodes.
-----------------------------------
<xsl:template match="/">

        <div id="rulesListTOC">
                <h2>Table of Contents:</h2>
                <ul>
                <xsl:for-each select="ruleItems">
                        <xsl:call-template name="rulelistTOC" />
                </xsl:for-each>
                </ul>
        </div>
        
</xsl:template>
-----------------------------------

For each parent level node it finds, it calls this template:
-----------------------------------
<xsl:template name="rulelistTOC">

        <xsl:variable name="TOCTemplateLoopCount"
select="$TOCTemplateLoopCount+1"/>

        <xsl:for-each select="ruleItem">

                <xsl:if test="$TOCTemplateLoopCount &lt; 3">

                <xsl:variable name="id">
                        <xsl:number level="multiple" from="/ruleItems"
/>
                </xsl:variable>

                        <li><a href="#id{$id}"><xsl:value-of
select="number" />: <xsl:value-of select="title" /></a>
                                <xsl:if test="ruleItem">
                                        <ul>
                                                <xsl:call-template
name="rulelistTOC" />
                                        </ul>
                                </xsl:if>
                        </li>
        
                </xsl:if>       
                
        </xsl:for-each>
</xsl:template>
-----------------------------------

I've attempted to put an xsl:if test in there to first check to see
which recursive loop it's on. If it's less than 3 (ie, first or second
pass through) then go ahead and render some markup.

The question I have is how to I initially set the TOCTemplateLoopCount
variable? I need to set it to 0 to begin with outside of this template.
However, if I do that...

<xsl:variable name="TOCTemplateLoopCount" select="0" />
<xsl:template name="rulelistTOC">
        <xsl:variable name="TOCTemplateLoopCount"
select="$TOCTemplateLoopCount+1"/>
        ...
</template>

It seems to get re-set to '0' on each pass of the template as it is
evaluated to '0 + 1' each time. I'm confused as to why that is.

-Darrel

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--