数据源XML,data.xml

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="style.xslt"?>
<Root>
    
<Troot>0</Troot>
    
<Troot>1</Troot>
    
<Troot>2</Troot>
    
<Troot>3</Troot>
    
<Troot>4</Troot>
    
<Troot>5</Troot>
    
<Troot>6</Troot>
    
<Troot>7</Troot>
    
<Troot>8</Troot>
    
<Troot>9</Troot>
    
<Troot>10</Troot>
    
<Troot>11</Troot>
    
<Troot>12</Troot>
    
<Troot>13</Troot>
    
<Troot>14</Troot>
    
<Troot>15</Troot>
    
<Troot>16</Troot>
    
<Troot>17</Troot>
    
<Troot>18</Troot>
    
<Troot>19</Troot>
    
<Troot>20</Troot>
</Root>


以下是XSLT的实现,style.xslt
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
<xsl:output method="html" version="4.0" encoding="GB2312"/>
    
<xsl:template match="/">
        
<html>
            
<head>
                
<title>XSLT版本斐波那契数列</title>
                
<style type="text/css">
                    
<xsl:comment><![CDATA[
                    span
                    {
                        font:9pt;
                        color:#999;
                        display:list-item;
                        list-style:none;
                    }
                    
]]></xsl:comment>
                
</style>
            
</head>
            
<body>
                
<span>斐波那契数列: </span>
                
<xsl:for-each select="Root/Troot">
                    
<span>
                        当n = 
<xsl:value-of select="." disable-output-escaping="no"/>时,结果:
                        
<xsl:call-template name="fun">
                            
<xsl:with-param name="n" select="."/>
                        
</xsl:call-template>
                    
</span>
                
</xsl:for-each>
            
</body>
        
</html>
    
</xsl:template>
    
<xsl:template name="fun">
        
<xsl:param name="n"/>
        
<xsl:choose>
            
<xsl:when test="$n &lt;= 1">
                
<xsl:value-of select="1" disable-output-escaping="no"/>
            
</xsl:when>
            
<xsl:otherwise>
                
<xsl:variable name="ni">
                    
<xsl:call-template name="fun">
                        
<xsl:with-param name="n" select="$n - 1"/>
                    
</xsl:call-template>
                
</xsl:variable>
                
<xsl:variable name="nii">
                    
<xsl:call-template name="fun">
                        
<xsl:with-param name="n" select="$n - 2"/>
                    
</xsl:call-template>
                
</xsl:variable>
                
<xsl:value-of select="number($nii) + number($ni)"/>
            
</xsl:otherwise>
        
</xsl:choose>
    
</xsl:template>
</xsl:stylesheet>


主要是为了验证XSLT中的template是可递归的,也可以类似一般函数一样有“返回值”,通过Variable。
不过,这个Variable实在是有些不通人情的,只能赋值一次,不可再做改动了!
posted on 2004-08-16 09:44  雪美·考拉  阅读(898)  评论(3编辑  收藏  举报