<xsl:template name="GCD"> <!-- The greatest common denominator (GCD) is the largest positive integer that divides into both numbers without a remainder. Examples: GCD(256,64)=64, GCD(12,8)=4, GCD(5,3)=1 --> <xsl:param name="x"/> <xsl:param name="y"/> <xsl:choose> <xsl:when test="$x < 0"> <!-- Call GCD with positive x --> <xsl:call-template name="GCD"> <xsl:with-param name="x" select="-$x"/> <xsl:with-param name="y" select="$y"/> </xsl:call-template> </xsl:when> <xsl:when test="$y < 0"> <!-- Call GCD with positive y --> <xsl:call-template name="GCD"> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="y" select="-$y"/> </xsl:call-template> </xsl:when> <xsl:when test="$x + $y > 0"> <!-- Valid input, call GCD-helper --> <xsl:call-template name="GCD-helper"> <xsl:with-param name="g" select="$y"/> <xsl:with-param name="x" select="$x"/> <xsl:with-param name="y" select="$y"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <!-- Error, both parameters zero --> <xsl:text>error</xsl:text> </xsl:otherwise> </xsl:choose> </xsl:template> <xsl:template name="GCD-helper"> <!-- Recursive template. Call until $x = 0. --> <xsl:param name="g"/> <xsl:param name="x"/> <xsl:param name="y"/> <xsl:choose> <xsl:when test="$x > 0"> <!-- Recursive call --> <xsl:call-template name="GCD-helper"> <xsl:with-param name="g" select="$x"/> <xsl:with-param name="x" select="$y mod $x"/> <xsl:with-param name="y" select="$x"/> </xsl:call-template> </xsl:when> <xsl:otherwise> <!-- End of recursion. GCD = $g --> <xsl:value-of select="$g"/> </xsl:otherwise> </xsl:choose> </xsl:template>
Hide code
Visustin flow chart for XSLT