ASPN ActiveState Programmer Network
ActiveState
/ Home / Perl / PHP / Python / Tcl / XSLT /
/ Safari / My ASPN /
Cookbooks | Documentation | Mailing Lists | Modules | News Feeds | Products | User Groups


Recent Messages
List Archives
About the List
List Leaders
Subscription Options

View Subscriptions
Help

View by Topic
ActiveState
.NET Framework
Open Source
Perl
PHP
Python
Tcl
Web Services
XML & XSLT

View by Category
Database
General
SOAP
System Administration
Tools
User Interfaces
Web Programming
XML Programming


MyASPN >> Mail Archive >> xsl-list
xsl-list
[xsl] Recursively looping through a template only X number of times?
by Darrel Austin other posts by this author
Nov 28 2006 9:05AM messages near this date
RE: [xsl] Simulate schema-awareness to create comboBoxes | RE: [xsl] Recursively looping through a template only X number of times?
& XSLT 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@[...].com> 
--~--
Thread:
Darrel Austin
Michael Kay
Brian Martinez
Darrel Austin
Darrel Austin

Privacy Policy | Email Opt-out | Feedback | Syndication
© ActiveState Software Inc. All rights reserved