Reputation: 3878
I am trying to understand recorsion in xslt. Can anybody explain what's happening in this code.
<xsl:template name="factorial">
<xsl:param name="number" select="1"/>
<xsl:choose>
<xsl:when test="$number <= 1">
<xsl:value-of select="1"/>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="recursive_result">
<xsl:call-template name="factorial">
<xsl:with-param name="number" select="$number - 1"/>
</xsl:call-template>
</xsl:variable>
<xsl:value-of select="$number * $recursive_result"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I can't understand why we wrap factorial template with <xsl:variable name="recursive_result">
.
If there is more clear example is available, please guide me to that. I am lack of knowledge in recursion.
Upvotes: 0
Views: 1656
Reputation: 666
In XSLT, we are using recursion instead of Looping. Recursion is nothing but a particular type of function that calls itself as many times when required to find the final solution. So,
$number
number
recursive_result
Hope would be understand.
Upvotes: 0
Reputation: 3714
You can't declare global variables in XSLT that are changeable from other parts of the script. If you need a result from a template call or a recursion is the only way to "print out" the generated result in a variable.
The "print out" is done with the <xsl:value-of ...
statement.
Upvotes: 1
Reputation: 6956
The call-template
element is wrapped with the variable
element in order to assign the result of calling it to the variable recursive_result
.
This is done so that it can then be multiplied by number
on the following line, to produce the final result.
Upvotes: 1