1<?xml version="1.0"?> 2 3<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 4 5 6 7<!-- Author: Chris Rathman --> 8 9<!-- Reference: http://www.angelfire.com/tx4/cus/notes/xslfactorial.html --> 10 11<!-- Description: Computes factorial as a demonstration of recursion --> 12 13 14 15<xsl:output method="text"/> 16 17 18 19<!-- x := 5 --> 20 21<xsl:variable name="x" select="5"/> 22 23 24 25<!-- y := factorial(x) --> 26 27<xsl:variable name="y"> 28 29<xsl:call-template name="factorial"> 30 31<xsl:with-param name="n" select="$x"/> 32 33</xsl:call-template> 34 35</xsl:variable> 36 37 38 39<!-- factorial(n) - compute the factorial of a number --> 40 41<xsl:template name="factorial"> 42 43<xsl:param name="n" select="1"/> 44 45<xsl:variable name="sum"> 46 47<xsl:if test="$n = 1">1</xsl:if> 48 49<xsl:if test="$n != 1"> 50 51<xsl:call-template name="factorial"> 52 53<xsl:with-param name="n" select="$n - 1"/> 54 55</xsl:call-template> 56 57</xsl:if> 58 59</xsl:variable> 60 61<xsl:value-of select="$sum * $n"/> 62 63</xsl:template> 64 65 66 67<!-- output the results --> 68 69<xsl:template match="/"> 70 71<xsl:text>factorial(</xsl:text> 72 73<xsl:value-of select="$x"/> 74 75<xsl:text>) = </xsl:text> 76 77<xsl:value-of select="$y"/> 78 79<xsl:text>
</xsl:text> 80 81 82 83<!-- calculate another factorial for grins --> 84 85<xsl:text>factorial(4) = </xsl:text> 86 87<xsl:call-template name="factorial"> 88 89<xsl:with-param name="n" select="5"/> 90 91</xsl:call-template> 92 93<xsl:text>
</xsl:text> 94 95</xsl:template> 96 97 <!-- 98 * Licensed to the Apache Software Foundation (ASF) under one 99 * or more contributor license agreements. See the NOTICE file 100 * distributed with this work for additional information 101 * regarding copyright ownership. The ASF licenses this file 102 * to you under the Apache License, Version 2.0 (the "License"); 103 * you may not use this file except in compliance with the License. 104 * You may obtain a copy of the License at 105 * 106 * http://www.apache.org/licenses/LICENSE-2.0 107 * 108 * Unless required by applicable law or agreed to in writing, software 109 * distributed under the License is distributed on an "AS IS" BASIS, 110 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 111 * See the License for the specific language governing permissions and 112 * limitations under the License. 113 --> 114 115</xsl:stylesheet> 116