Recursion

Recursion is a programming technique in which a function may call itself. Recursive programming is especially well-suited to parsing nested markup structures such as XML/XSL.

According to WIKIPEDIA:

Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. The term is also used more generally to describe a process of repeating objects in a self-similar way. For instance, when the surfaces of two mirrors are almost parallel with each other the nested images that occur are a form of recursion.
Recursion in XSL:
Example XML:
<?xml version="1.0" encoding="UTF-8"?>
<sitemap>
	<page>
		<url>/page1.html</url>
		<text>Page 1</text>
		<page>
			<url>/page1-1.html</url>
			<text>Page 1-1</text>		
			<page>
				<url>/page1-1-1.html</url>
				<text>Page 1-1-1</text>
			</page>
		</page>
		<page>
			<url>/page1-2.html</url>
			<text>Page 1-2</text>
		</page>
	</page>
	<page>
		<url>/page2.html</url>
		<text>Page 2</text>
		<page>
			<url>/page2-1.html</url>
			<text>Page 2-1</text>
		</page>
		<page>
			<url>/page2-2.html</url>
			<text>Page 2-2</text>
		</page>
	</page>
	<page>
		<url>/page3html</url>
		<text>Page 3</text>
	</page>
	<page>
		<url>/page4.html</url>
		<text>Page 4</text>
	</page>
</sitemap>
Example XSL:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" media-type="html"/>
	<xsl:template match="sitemap">
	<xsl:call-template name="display">
	<xsl:with-param name="count" select="count(page/*)"/>
	</xsl:call-template>
	</xsl:template>
	
	<xsl:template name="display">
	  <xsl:param name="count"/>
	  <xsl:if test="$count > 0">
		  <xsl:for-each select="page">
		  <a>
			  <xsl:attribute name="href">
			  <xsl:value-of select="url"/>
			  </xsl:attribute>
			  <xsl:value-of select="text"/>
		  </a>
		  <xsl:call-template name="display">
		  <xsl:with-param name="count" select="$count - 1"/>
		  </xsl:call-template>
		  </xsl:for-each>
	  </xsl:if>		  	  
	</xsl:template>
</xsl:stylesheet>
Example Output:
<?xml version="1.0" encoding="UTF-8"?>
<a href="/page1.html">Page 1</a>
<a href="/page1-1.html">Page 1-1</a>
<a href="/page1-1-1.html">Page 1-1-1</a>
<a href="/page1-1-1-1.html">Page 1-1-1-1</a>
<a href="/page1-2.html">Page 1-2</a>
<a href="/page2.html">Page 2</a>
<a href="/page2-1.html">Page 2-1</a>
<a href="/page2-2.html">Page 2-2</a>
<a href="/page3html">Page 3</a>
<a href="/page4.html">Page 4</a>
line

Services Web Design Content Management Consulting Services