Recursion
XSLT depends on recursion as a looping mechanism. Recursion occurs when a section of code calls itself, either directly or indirectly. Both named and unnamed templates can use recursion, and different templates can use mutual recursion, one calling another that in turn calls the first.
To avoid infinite recursion and excessive consumption of system resources, the JUNOS management process (mgd) limits the maximum recursion to 5000 levels. If this limit is reached, the script fails.
In the following example, an unnamed template matches on a
<count>element. It then calls the<count-to-max>template, passing the value of that element asmax. The<count-to-max>template starts by declaring both themaxandcurparameters and setting the default value of each to1(one). Then the current value of$curis emitted in an<out>element. Finally, if$curis less than$max, the<count-to-max>template recursively invokes itself, passing$cur + 1ascur. This recursive pass then outputs the next number and repeats the recursion until$curequals$max.<xsl:template match="count"><xsl:call-template name="count-to-max"><xsl:with-param name="max" select="count"/></xsl:call-template></xsl:template><xsl:template name="count-to-max"><xsl:param name="cur" select="'1'"/><xsl:param name="max" select="'1'"/><out><xsl:value-of select="$cur"/></out><xsl:if test="$cur < $max"><xsl:call-template name="count"><xsl:with-param name="cur" select="$cur + 1"/><xsl:with-param name="max" select="$max"/></xsl:call-template></xsl:if></xsl:template>Given a
maxvalue of10, the values contained in the<out>tag are1,2,3,4,5,6,7,8,9, and10.