Altova Mailing List Archives

[xsl] How is memory allocated in recursive XSLT templates?

From: "Rashmi Rubdi" <rashmi.sub@--------->
Date: 5/2/2007 6:35:00 PM
Hello Everyone,

Please treat this as a low priority, academic question.

I am interested in finding out what data structure is used internally
to represent each template's data in situations where the template is
called recursively, because I was thinking that infinite recursion is
not possible if memory is allocated in the form of a stack.

I would like to think of templates as equivalent to a function/ method
in other programming languages for the purpose of this post.

I was reading on recursive functions, and learned that each function
call's data is stored in a stack frame , and at the end of the last
function call, the data of each function call is poped out of the
stack frame and returned in reverse order --- LIFO.

If a stack is used then, it poses memory constraints when stack frames
run out, this is why it is important to have a termination condition.
And when there's a termination condition it is not infinite recursion.

In case of XSLT the termination condition is the depth of an XML
structure (it cannot be infinite),  or it could be a constraint
specified by the author of the XSLT stylesheet.

There's an illustration which shows how memory is allocated in a stack
frame for each function call at the bottom of this page :

I made a naive attempt to calculate the factorial of a given number
recursively with XSL templates, but soon realized that there's no
return statement.

I know that XSLT has functions, I haven't read about them yet, but I
plan to soon.

Here's what I tried (it's incomplete):

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="">

<xsl:template name="factorialTemplate">
	<xsl:param name="n"/>	
		<xsl:when test="$n=0">
			<xsl:call-template name="factorialTemplate">
				<xsl:with-param name="n" select="$n-1"/>


I was trying to achieve the equivalent of the recursive Factorial
function illustrated here with procedural programming:

Any thoughts on whether the memory is allocated in terms of Stack with
recursive template/ or recursive XLST functions is appreciated.

edit: before I hit the send button, I realized that Google has some
info on this, I plan to read

-Thank you


These Archives are provided for informational purposes only and have been generated directly from the Altova mailing list archive system and are comprised of the lists set forth on Therefore, Altova does not warrant or guarantee the accuracy, reliability, completeness, usefulness, non-infringement of intellectual property rights, or quality of any content on the Altova Mailing List Archive(s), regardless of who originates that content. You expressly understand and agree that you bear all risks associated with using or relying on that content. Altova will not be liable or responsible in any way for any content posted including, but not limited to, any errors or omissions in content, or for any losses or damage of any kind incurred as a result of the use of or reliance on any content. This disclaimer and limitation on liability is in addition to the disclaimers and limitations contained in the Website Terms of Use and elsewhere on the site.