随笔 - 124  文章 - 0 评论 - 511 trackbacks - 9
<2004年8月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
2930311234

与我联系

搜索

 

常用链接

留言簿(10)

我参加的小组

我参与的团队

随笔分类

随笔档案

相册

收藏夹

博客链接

技术站点

社区推荐

积分与排名

  • 积分 - 172372
  • 排名 - 209

最新评论

阅读排行榜

评论排行榜

XML数据源文件和第一版的一样!
这个XSLT解决没有递归出节点属性值总和的问题
不过是借助JS实现的
不管怎么样,问题算是解决了!



<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
<!-- 这个名称空间可以使用output,但是IE5不可解析 -->
    
<!-- 推荐使用环境,MSIE6,MSXML4.0 -->
    
<xsl:output method="html" version="4.0" encoding="GB2312" />
    
<xsl:template match="/">
        
<html>
            
<head>
                
<title>XMLTREE</title>
                
<style>
                    
<xsl:comment>
                
<![CDATA[
                h1
                {
                    display:list-item;
                    padding:2px;
                    list-style:none;
                }
                span.clsP
                {
                    list-style-type:square;
                    color:#222;
                }
                span.clsC
                {
                    list-style-type:disc;
                    color: #00b;
                }
                span
                {
                    padding: 2px;
                    font: 9pt;
                    cursor: default;
                    text-decoration:none;
                    margin: 0px;
                    margin-left: 16px;
                }
                div
                {
                    margin-left: 18px;
                    border-left: 1px solid #ddd;
                    border-bottom:1px solid;
                    border-bottom-color:expression(document.bgColor);
                    display:block;
                }                
]]>
                
</xsl:comment>
                
</style>
            
</head>
            
<body onselectstart="return false;">
                
<xsl:call-template name="NextPID">
                    
<xsl:with-param name="mPID">-1</xsl:with-param>
                    
<xsl:with-param name="mNum">0</xsl:with-param>
                
</xsl:call-template>
            
</body>
            
<script language="javascript">
                
<xsl:comment>
            
<![CDATA[
            //************************************************
            //
            //      解决没有递归出节点属性值总和的问题
            //
            //************************************************
            
            var i, j, nodesum;
            
            //因为提前把一个根下的所有孩子都方在一个DIV内
            //所以把这些DIV都找出来就可以找到一个根下的所有孩子了
            var objs = document.body.getElementsByTagName("DIV");
            
            for(i = 0; i < objs.length; i++)
            {
                var obj = objs[i];
                
                // 找到这些孩子的父亲
                var nobj = document.getElementById("snode" + obj.id.replace("node",""));
                
                // 初始根节点下孩子总和的值为此节点附有的同属性的值
                nodesum = parseInt(nobj.mc, 10);

                // 所有带有目标属性的同宗的孩子
                var spanobjs = obj.getElementsByTagName("SPAN");
                
                for(j in spanobjs)
                {
                    if(spanobjs[j].mc)    // 去除一些干扰
                        nodesum += parseInt(spanobjs[j].mc, 10);    // 累加
                }
                // 可以写祖宗的innerText了!成功!
                nobj.innerText += "(" + (nodesum) + ")";
                
            }
        
            //  就这样,把所有需要计算的东西都放到Client去做
            //  服务器应该尽可能闲下来休息一下的
            
]]>
            
</xsl:comment>
            
</script>
        
</html>
    
</xsl:template>
    
<xsl:template name="NextPID">
        
<xsl:param name="mPID" />
        
<xsl:param name="mNum" />
        
<xsl:for-each select="//Troot/Item[@pid = $mPID]">
            
<xsl:sort select="count(//Troot/Item[@pid = current()/@id])" order="descending" data-type="number" />
            
<!-- 首先按拥有孩子的数量来排序 -->
            
<xsl:sort select="@id" order="ascending" />
            
<xsl:choose>
                
<xsl:when test="count(//Troot/Item[@pid = current()/@id]) &gt; 0">
                    
<!-- 有孩子的节点 -->
                    
<h1>
                        
<span class="clsP">
                            
<xsl:attribute name="id">snode<xsl:value-of select="@id" /></xsl:attribute>
                            
<xsl:attribute name="mc">
                                
<xsl:value-of select="@c" />
                            
</xsl:attribute>
                            
<xsl:value-of select="." />
                            
<!-- [下属节点数:<xsl:value-of select="format-number(count(//Troot/Item[@pid = current()/@id]),'00')" />] -->
                        
</span>
                    
</h1>
                    
<div>
                        
<xsl:attribute name="id">node<xsl:value-of select="@id" /></xsl:attribute>
                        
<xsl:call-template name="NextPID">
                            
<xsl:with-param name="mPID">
                                
<xsl:value-of select="@id" />
                            
</xsl:with-param>
                            
<xsl:with-param name="mNum">
                                
<xsl:value-of select="$mNum + @c" />
                            
</xsl:with-param>
                        
</xsl:call-template>
                    
</div>
                
</xsl:when>
                
<xsl:otherwise>
                    
<!-- 孤单的节点 -->
                    
<h1>
                        
<span class="clsC">
                            
<xsl:attribute name="snode">
                                
<xsl:value-of select="@id" />
                            
</xsl:attribute><xsl:attribute name="mc">
                                
<xsl:value-of select="@c" />
                            
</xsl:attribute>
                            
<xsl:value-of select="." />(<xsl:value-of select="@c" />)
                        
</span>
                    
</h1>
                
</xsl:otherwise>
            
</xsl:choose>
        
</xsl:for-each>
    
</xsl:template>
</xsl:stylesheet>
<!-- 最终版权归 DSclub(任兀)拥有,您可以在未授权的情况下使用,但请保留此信息 -->
<!--
    EMail:dsclub@hotmail.com
    QQ:9967030
    Nick Name: DSclub(兀儿-干部)
    姓名:任兀
    性别:男生(未婚哦)
-->
posted @ 2004-08-16 23:56 搏软狂歌 阅读(804) | 评论 (0)编辑
     摘要: 数据文件:xmltree.xml<?xmlversion="1.0"encoding="GB2312"?><?xml-stylesheettype="text/xsl"href="style.xsl"?><Troot><Itemid="1"pid="0"c="1">大学</Item><Itemid="2"pid="0"c="3"&g... 阅读全文
posted @ 2004-08-16 18:48 搏软狂歌 阅读(1520) | 评论 (7)编辑
数据源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 @ 2004-08-16 09:44 搏软狂歌 阅读(657) | 评论 (3)编辑