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实用教程_第8章_XSLT、XPath转换XML文件.doc
- **调试XSLT**:XMLSpy提供了丰富的调试工具,可以帮助开发者检查XSLT转换过程中可能出现的问题。
xslt 中将<br/>解析成换行
例如,如果`<br/>`标签可能包含其他元素,我们可以创建一个模板来递归处理这些子节点:```xml<xsl:template match="br"> <xsl:text>\n</xsl:text> <
xsl递归解晰xml
总结来说,“xsl递归解析xml”是将XML数据通过XSLT转换成其他格式的过程,其中递归模板是处理XML树形结构的关键。这个过程可以与JavaScript和HTML结合,实现动态交互式的数据展示。
XSL中进行字符串替换
通过递归地调用自身,模板可以有效地替换所有的目标子字符串。最后,我们通过`match="/"`的主模板将这个替换功能应用到了实际的数据上。


