+ -
当前位置:首页 → 问答吧 → xsl 实现递归树的问题,急急急!!!!

xsl 实现递归树的问题,急急急!!!!

时间:2004-07-10

来源:互联网

现有这样一个xml文件
<Tree>
<Leave id="10020001">
<Desc>银行存款</Desc>
<Values>16483103.69</Values>
<Parent>10020001</Parent>
</Leave>

<Leave id="10020002">
<Desc>活期存款</Desc>
<Values>16483103.69</Values>
<Parent>10020001</Parent>
</Leave>

<Leave id="10020003">
<Desc>人民银行存款</Desc>
<Values></Values>
<Parent>10020002</Parent>
</Leave>

<Leave id="10020004">
<Desc>中资银行</Desc>
<Values></Values>
<Parent>10020002</Parent>
</Leave>

这个xml的结构是这样的
每个Leave都有 id 属性 唯一表示该 节点
同时也有Parent 属性,该属性记载该节点父节点的 id 属性值
如果 Parent 的值 跟 id 属性的值相同则代表 该节点没有父节点


我要实现在xsl样式表中能够自动通过<Parent>标签 将一个<Leave>挂到另一个<Leave>标签(这个Leave 的 id 值等于前一个Leave的 Parent 值)下面,从而生成一棵树

我是这样写的,但总是实现不了,而且也有错,还请各位高手指教:

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" language="JavaScript">
<xsl:template match="Tree">
<!--首先显示所有根节点即 @id=parent-->
<xsl:for-each match=".[Leave/@id=Leave/Parent]">   
  <xsl:apply-templates select="Leave"/>
</xsl:for-each>
</xsl:template>

<xsl:template match="Leave">
  <div onclick="window.event.cancelBubble = true;clickOnEntity(this);" onselectstart="return false" ondragstart="return false">
  <xsl:attribute name="image">images/book.gif</xsl:attribute>
  <xsl:attribute name="imageOpen">images/bookOpen.gif</xsl:attribute>
  <xsl:attribute name="open">false</xsl:attribute>
  <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute>
  <xsl:attribute name="open">false</xsl:attribute>
  <xsl:attribute name="STYLE">
    padding-left: 20px;
    cursor: hand;
<!--果不是根节点就隐藏-->
    <xsl:if match=".[Parent!=@id]">
      display: none;
    </xsl:if>
  </xsl:attribute>
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td valign="middle">
          <img border="0" id="image">
            <xsl:attribute name="SRC">images/bookOpen.gif</xsl:attribute>
          </img>
        </td>
        <td valign="middle" nowrap="true">
        <xsl:attribute name="STYLE">
          padding-left: 7px;
          font-family: Verdana;
          font-size: 9pt;
          font-color: black;
        </xsl:attribute>
        <xsl:value-of select="Desc"/><xsl:value-of select="Values"/></td>
      </tr>
    </table>
<!--查找当前 Leave 的子 Leave  
即查找 Parent = 当前Leave的 @id 的 Leave-->

    <xsl:for-each select="Leave">
               <!--如果当前@id!=Parent  且当前 Parent=上一个@id-->
        <xsl:if match=".[Parent!=@id and Parent=../@id]">
                <xsl:apply-templates select="Leave"/>
        </xsl:if>
    </xsl:for-each>
  </div>
</xsl:template>

</xsl:stylesheet>

[ 本帖由 sc_80817 于 2004-7-10 14:38 最后编辑 ]

作者: sc_80817   发布时间: 2004-07-10

我也想了解这个

作者: sendidmax   发布时间: 2005-09-20


<?xml version="1.0" encoding="gb2312"?> <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/Tree"> <!--首先显示所有根节点即 @id=parent--> <body onclick="alert(this.innerHTML);"> <xsl:apply-templates select="Leave"/> </body> </xsl:template> <xsl:template match="Leave"> <div onclick="window.event.cancelBubble = true;clickOnEntity(this);" onselectstart="return false" ondragstart="return false"> <xsl:attribute name="image">images/book.gif</xsl:attribute> <xsl:attribute name="imageOpen">images/bookOpen.gif</xsl:attribute> <xsl:attribute name="open">false</xsl:attribute> <xsl:attribute name="id"><xsl:value-of select="@id"/></xsl:attribute> <xsl:attribute name="open">false</xsl:attribute> <xsl:attribute name="STYLE"> padding-left: 20px; cursor: hand; <!--果不是根节点就隐藏--> <xsl:if test="Parent!=@id"> display: none; </xsl:if> </xsl:attribute> <table border="0" cellspacing="0" cellpadding="0"> <tr> <td valign="middle"> <img border="0" id="image"> <xsl:attribute name="SRC">images/bookOpen.gif</xsl:attribute> </img> </td> <td valign="middle" nowrap="true"> <xsl:attribute name="STYLE"> padding-left: 7px; font-family: Verdana; font-size: 9pt; font-color: black; </xsl:attribute> <xsl:value-of select="Desc"/><xsl:value-of select="Values"/></td> </tr> </table> <!--查找当前 Leave 的子 Leave 即查找 Parent = 当前Leave的 @id 的 Leave--> <xsl:apply-templates select="Leave"/> </div> </xsl:template> </xsl:stylesheet>
   提示:您可以先修改部分代码再运行

作者: sendidmax   发布时间: 2005-09-20