Skip to content

Instantly share code, notes, and snippets.

@welblaud
Created December 12, 2018 08:58
Show Gist options
  • Save welblaud/b3689adc9cd347b1d5322a91260edfbe to your computer and use it in GitHub Desktop.
Save welblaud/b3689adc9cd347b1d5322a91260edfbe to your computer and use it in GitHub Desktop.
XSLT recursive template call – calculating factorial
<!-- Factorial calculated with XSLT recursion. -->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="text"/>
<xsl:param name="number" select="9"/>
<xsl:template match="/">
<xsl:call-template name="factorial">
<xsl:with-param name="n" select="$number"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="factorial">
<xsl:param name="n"/>
<xsl:variable name="m">
<xsl:if test="$n &lt;= 1">
<xsl:text>1</xsl:text>
</xsl:if>
<xsl:if test="$n > 1">
<xsl:call-template name="factorial">
<xsl:with-param name="n" select="$n - 1"/>
</xsl:call-template>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$m * $n"/>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment