请XSLT高手关注!不太好解决的问题!

有XML文档,型如:

1<root>
2<node id="" pid="" value="">M</node>   
3....   
4</root>

用XSLT可以把他生成一棵树!

如下:
N
&brvbar---Y
&brvbar---4
&brvbar---5
&brvbar---M
&brvbar---6
&brvbar---7
&brvbar---1
&brvbar---2
&brvbar---3

现在是如何能让各个根节点的值等于其下所有叶子节点的值的总和!??
比如:

Y = 4 + 5 = 9
M = 6 + 7 = 13
N = Y + M + 1 + 2 + 3 = 9 + 13 + 1 + 2 + 3 = 28

要怎么写呢?

解决后再开200分答谢!!
---------------------------------------------------------------
解出来了!!!!

靠,累死我了!(还是自己太笨!)

XSLT实现XML无极限树(精简版)[二] 解决没有递归出节点属性值总和的问题(JS实现)
http://www.cnblogs.com/dsclub/archive/2004/08/16/33959.aspx

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

数据文件:
xmltree.xml

 1<troot>
 2<item c="1" id="1" pid="0">大学</item>
 3<item c="3" id="2" pid="0">中学</item>
 4<item c="3" id="3" pid="0">小学</item>
 5<item c="2" id="4" pid="2">高中</item>
 6<item c="5" id="5" pid="2">初中</item>
 7<item c="3" id="6" pid="15">清华大学</item>
 8<item c="4" id="7" pid="15">北京大学</item>
 9<item c="3" id="8" pid="5">天津铁三中</item>
10<item c="3" id="9" pid="4">天津市二中</item>
11<item c="2" id="10" pid="16">天津音乐学院</item>
12<item c="5" id="11" pid="15">天津商学院</item>
13<item c="3" id="12" pid="4">耀华中学</item>
14<item c="6" id="13" pid="3">昆纬路小学</item>
15<item c="6" id="14" pid="2">七中</item>
16<item c="1" id="15" pid="1">综合类院校</item>
17<item c="1" id="16" pid="1">艺术类院校</item>
18<item c="4" id="17" pid="15">医科大学</item>
19<item c="4" id="18" pid="15">天津师范大学</item>
20<item c="23" id="19" pid="15">天津大学</item>
21<item c="7" id="20" pid="15">南开大学</item>
22<item c="23" id="21" pid="4">天津铁一中</item>
23<item c="5" id="22" pid="5">天津铁一中</item>
24<item c="3" id="23" pid="3">天津市铁路职工子弟第三小学</item>
25<item c="3" id="24" pid="3">天津市铁路职工子弟第一小学</item>
26<item c="3" id="25" pid="16">美术学院</item>
27<item c="3" id="26" pid="16">体育学院</item>
28<item c="2" id="0" pid="-1">学校</item>
29</troot>

XSLT: style.xsl

  1<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2<!-- 这个名称空间可以使用output,但是IE5不可解析 -->
  3<!-- 推荐使用环境,MSIE6,MSXML4.0 -->
  4<xsl:output encoding="GB2312" method="html" version="4.0"></xsl:output>
  5<xsl:template match="/">
  6<html>
  7<head>
  8<title>XMLTREE</title>
  9<style>   
 10<xsl:comment>   
 11&lt;![CDATA[   
 12h1   
 13{   
 14display:list-item;   
 15padding:2px;   
 16list-style:none;   
 17}   
 18span.clsP   
 19{   
 20list-style-type:square;   
 21color:#222;   
 22}   
 23span.clsC   
 24{   
 25list-style-type:disc;   
 26color: #00b;   
 27}   
 28span   
 29{   
 30padding: 2px;   
 31font: 9pt;   
 32cursor: default;   
 33text-decoration:none;   
 34margin: 0px;   
 35margin-left: 16px;   
 36}   
 37div   
 38{   
 39margin-left: 18px;   
 40border-left: 1px solid #ddd;   
 41border-bottom:1px solid;   
 42border-bottom-color:expression(document.bgColor);   
 43display:block;   
 44} ]]&gt;   
 45</xsl:comment>   
 46</style>
 47</head>
 48<body onselectstart="return false;">
 49<xsl:call-template name="NextPID">
 50<xsl:with-param name="mPID">-1</xsl:with-param>
 51<xsl:with-param name="mNum">0</xsl:with-param>
 52</xsl:call-template>
 53</body>
 54<script language="javascript">   
 55<xsl:comment>   
 56&lt;![CDATA[   
 57//************************************************   
 58//   
 59// 解决没有递归出节点属性值总和的问题   
 60//   
 61//************************************************   
 62  
 63var i, j, nodesum;   
 64  
 65//因为提前把一个根下的所有孩子都方在一个DIV内   
 66//所以把这些DIV都找出来就可以找到一个根下的所有孩子了   
 67var objs = document.body.getElementsByTagName("DIV");   
 68  
 69for(i = 0; i < objs.length; i++)   
 70{   
 71var obj = objs[i];   
 72  
 73// 找到这些孩子的父亲   
 74var nobj = document.getElementById("snode" + obj.id.replace("node",""));   
 75  
 76// 初始根节点下孩子总和的值为此节点附有的同属性的值   
 77nodesum = parseInt(nobj.mc, 10);   
 78  
 79// 所有带有目标属性的同宗的孩子   
 80var spanobjs = obj.getElementsByTagName("SPAN");   
 81  
 82for(j in spanobjs)   
 83{   
 84if(spanobjs[j].mc) // 去除一些干扰   
 85nodesum += parseInt(spanobjs[j].mc, 10); // 累加   
 86}   
 87// 可以写祖宗的innerText了!成功!   
 88nobj.innerText += "(" + (nodesum) + ")";   
 89  
 90}   
 91  
 92// 就这样,把所有需要计算的东西都放到Client去做   
 93// 服务器应该尽可能闲下来休息一下的   
 94]]&gt;   
 95</xsl:comment>   
 96</script>
 97</html>
 98</xsl:template>
 99<xsl:template name="NextPID">
100<xsl:param name="mPID"></xsl:param>
101<xsl:param name="mNum"></xsl:param>
102<xsl:for-each select="//Troot/Item[@pid = $mPID]">
103<xsl:sort data-type="number" order="descending" select="count(//Troot/Item[@pid = current()/@id])"></xsl:sort>
104<!-- 首先按拥有孩子的数量来排序 -->
105<xsl:sort order="ascending" select="@id"></xsl:sort>
106<xsl:choose>
107<xsl:when test="count(//Troot/Item[@pid = current()/@id]) &gt; 0">
108<!-- 有孩子的节点 -->
109<h1>
110<span class="clsP">
111<xsl:attribute name="id">snode<xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
112<xsl:attribute name="mc">
113<xsl:value-of select="@c"></xsl:value-of>
114</xsl:attribute>
115<xsl:value-of select="."></xsl:value-of>
116<!-- [下属节点数:<xsl:value-of select="format-number(count(//Troot/Item[@pid = current()/@id]),'00')" />] -->
117</span>
118</h1>
119<div>
120<xsl:attribute name="id">node<xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
121<xsl:call-template name="NextPID">
122<xsl:with-param name="mPID">
123<xsl:value-of select="@id"></xsl:value-of>
124</xsl:with-param>
125<xsl:with-param name="mNum">
126<xsl:value-of select="$mNum + @c"></xsl:value-of>
127</xsl:with-param>
128</xsl:call-template>
129</div>
130</xsl:when>
131<xsl:otherwise>
132<!-- 孤单的节点 -->
133<h1>
134<span class="clsC">
135<xsl:attribute name="snode">
136<xsl:value-of select="@id"></xsl:value-of>
137</xsl:attribute><xsl:attribute name="mc"></xsl:attribute></span></h1></xsl:otherwise></xsl:choose></xsl:for-each></xsl:template></xsl:stylesheet>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus