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 on 2004-08-16 23:56  雪美·考拉  阅读(1498)  评论(0编辑  收藏  举报