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> 
(注:以上代码可能来源于网络,这是我在我的电脑上找出来的代码。呵呵)

相关文章:

  • 2021-04-29
  • 2021-06-25
  • 2021-12-05
  • 2022-01-27
  • 2021-08-02
  • 2021-11-20
  • 2021-11-12
猜你喜欢
  • 2021-07-16
  • 2022-12-23
  • 2022-01-21
  • 2021-11-27
  • 2021-04-06
  • 2022-12-23
  • 2021-09-03
相关资源
相似解决方案