XSLT实现XML无极限树(精简版)[一]代码少,传输好!

数据文件:
xmltree.xml
xml version="1.0" encoding="GB2312"  ?>
xml-stylesheet type="text/xsl" href="style.xsl" ?>
< Troot >
    
< Item  id ="1"  pid ="0"  c ="1" > 大学 Item >
    
< Item  id ="2"  pid ="0"  c ="3" > 中学 Item >
    
< Item  id ="3"  pid ="0"  c ="3" > 小学 Item >
    
< Item  id ="4"  pid ="2"  c ="2" > 高中 Item >
    
< Item  id ="5"  pid ="2"  c ="5" > 初中 Item >
    
< Item  id ="6"  pid ="15"  c ="3" > 清华大学 Item >
    
< Item  id ="7"  pid ="15"  c ="4" > 北京大学 Item >
    
< Item  id ="8"  pid ="5"  c ="3" > 天津铁三中 Item >
    
< Item  id ="9"  pid ="4"  c ="3" > 天津市二中 Item >
    
< Item  id ="10"  pid ="16"  c ="2" > 天津音乐学院 Item >
    
< Item  id ="11"  pid ="15"  c ="5" > 天津商学院 Item >
    
< Item  id ="12"  pid ="4"  c ="3" > 耀华中学 Item >
    
< Item  id ="13"  pid ="3"  c ="6" > 昆纬路小学 Item >
    
< Item  id ="14"  pid ="2"  c ="6" > 七中 Item >
    
< Item  id ="15"  pid ="1"  c ="1" > 综合类院校 Item >
    
< Item  id ="16"  pid ="1"  c ="1" > 艺术类院校 Item >
    
< Item  id ="17"  pid ="15"  c ="4" > 医科大学 Item >
    
< Item  id ="18"  pid ="15"  c ="4" > 天津师范大学 Item >
    
< Item  id ="19"  pid ="15"  c ="23" > 天津大学 Item >
    
< Item  id ="20"  pid ="15"  c ="7" > 南开大学 Item >
    
< Item  id ="21"  pid ="4"  c ="23" > 天津铁一中 Item >
    
< Item  id ="22"  pid ="5"  c ="5" > 天津铁一中 Item >
    
< Item  id ="23"  pid ="3"  c ="3" > 天津市铁路职工子弟第三小学 Item >
    
< Item  id ="24"  pid ="3"  c ="3" > 天津市铁路职工子弟第一小学 Item >
    
< Item  id ="25"  pid ="16"  c ="3" > 美术学院 Item >
    
< Item  id ="26"  pid ="16"  c ="3" > 体育学院 Item >
    
< Item  id ="0"  pid ="-1"  c ="2" > 学校 Item >
Troot >



XSLT: style.xsl
< 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 > XMLTREE title >
                
< style >
                    
< xsl:comment >
                

                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 >
        
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]) > 0" >
                    

                    
< h1 >
                        
< span  class ="clsP" >
                            
< xsl:attribute  name ="snode" >
                                
< xsl:value-of  select ="@id"   />
                            
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:value-of  select ="."   /> [包含内容: < xsl:value-of  select ="@c"   /> ]
                        
span >
                    
h1 >
                
xsl:otherwise >
            
xsl:choose >
        
xsl:for-each >
    
xsl:template >
xsl:stylesheet >




对于单表的数据库,可以用ASP在Server上解析中间XML层,再用XSLT!
使用是可以自己根据喜好添加htc文件,和CSS!

这是一个很基础的版本,后期我还会不断升级它的功能!

你可能感兴趣的:(XSLT实现XML无极限树(精简版)[一]代码少,传输好!)