利用XSLT来求得Fibonacci数列问题

        Fibonacci数列表示的是除第1、2位数字外,从第三位开始每一位都是前两位之和,例如:1,1,2,3,5,8,13...
        XSLT(XSLT is a language for transforming XML documents into XHTML documents or to other XML documents)是转换XML的语言。在XSLT中有一些基本流程控制例如<xsl:if ><xsl:choose><xsl:when><xsl:otherwise>等。
        我们知道要实现Fibonacci数列可以通过循环的方式或者递归调用的方式,下面给出利用XSLT来输出Fibonacci数列以及求Fibonacci的第n项值。(需要注意的是由于Fibonacci数列数值增长的非常快,所以n尽量小点)
        1、输出Fibonacci数列
        <?xml version="1.0" encoding="utf-8"?>
        <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">  
                <xsl:call-template name="repeat"> 
                       <xsl:with-param name="times" select="5" /> 
                </xsl:call-template> 
        </xsl:template> 
        <xsl:template name="repeat"> 
               <xsl:param name="times" select="0" />
               <xsl:param name="f1" select="1" />
               <xsl:param name="f2" select="1" />
               <xsl:if test="$times &gt 0"> 
                   <xsl:value-of select="$f1" />
                   <xsl:text> , </xsl:text>
                   <xsl:value-of select="$f2" />
                   <xsl:text> ,</xsl:text>
                   <xsl:call-template name="repeat">
                            <xsl:with-param name="times" select="$times - 1" />
                            <xsl:with-param name="f1" select="$f1+$f2" />
                            <xsl:with-param name="f2" select="$f2+$f1+$f2" />
                   </xsl:call-template> 
              </xsl:if> 
        </xsl:template>
        </xsl:stylesheet> 
        2、Fibonacci数列第n项的值
         <?xml version="1.0" encoding="utf-8"?>
        <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:template match="/">
             <xsl:call-template name="fibonacci">
                    <xsl:with-param name="n" select="$n"/>
             </xsl:call-template>
        </xsl:template>

      <xsl:template name="fibonacci">
          <xsl:param name="n" />
          <xsl:choose>
                     <xsl:when test="$n &lt;= 0">
                            <xsl:text>0</xsl:text>
                     </xsl:when>

                     <xsl:when test="$n = 1">
                            <xsl:text>1</xsl:text>
                     </xsl:when>
                     <xsl:otherwise>
                               <xsl:call-template name="fiboloop">
                                         <xsl:with-param name="fn1" select="1"/>
                                         <xsl:with-param name="fn2" select="0"/>
                                         <xsl:with-param name="remains" select="$n - 2"/>
                               </xsl:call-template>
                       </xsl:otherwise>
              </xsl:choose>
        </xsl:template>

 
      <xsl:template name="fiboloop">
          <xsl:param name="fn1"/>
          <xsl:param name="fn2"/>
          <xsl:param name="remains"/>
          <xsl:variable name="current" select="$fn1 + $fn2"/>
          <xsl:choose>
                     <xsl:when test="$remains = 0">
                            <xsl:value-of select="$current"/>
                     </xsl:when>
                     <xsl:otherwise>
                               <xsl:call-template name="fiboloop">
                                         <xsl:with-param name="fn1" select="$current"/>
                                         <xsl:with-param name="fn2" select="$fn1"/>
                                         <xsl:with-param name="remains" select="$remains - 1"/>
                               </xsl:call-template>
                      </xsl:otherwise>
                </xsl:choose>
          </xsl:template>
       </xsl:stylesheet> 
(注:以上代码可能来源于网络,这是我在我的电脑上找出来的代码。呵呵)

posted @ 2007-05-16 10:31  agaric  阅读(388)  评论(0编辑  收藏  举报