xsl递归问题
现有这样一个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 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>
<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 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>
...全文
相关推荐
javascript xml xsl取值及数据修改第1/2页
前几天公司要用xml快速方便的存取属性N多的数据,跟sinoly合作研究了一下.做了一个小例子.
xml实用教程_第8章_XSLT、XPath转换XML文件.doc
xml实用教程_第8章_XSLT、XPath转换XML文件教程,好东西,包你学会xml
xslt 中将<br/>解析成换行
NULL 博文链接:https://baobeituping.iteye.com/blog/753598
xsl递归解晰xml
NULL 博文链接:https://bbjava.iteye.com/blog/1262080
XSL中进行字符串替换
稍熟悉XPATH的人都知道,在XSL中使用translate函数可以将一个字符串进行逐字符替换。但是,如果要替换一个子字符串为另一个子字符串,对XPATH1.0而言,用translate就有点力不从心了。 一般来讲,在XSL中进行字符串替换有以下两种方法: (1)使用ms:script嵌入XSL实现,但要求XML解析器必须是MS的。 (2)使用纯XSL模板来实现。 当然,使用第(1)种方法有个好处就是可以充分利用javascript强大的字符串处理功能,例如还可以使用正则表达式。这里使用了一个纯XSL模板来实现字符串替换。


