以文本方式查看主题

-  中文XML论坛 - 专业的XML技术讨论区  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  xml+xslt中检索父ID[推荐]  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=7643)


--  作者:宇宙人
--  发布时间:5/18/2004 5:11:00 PM

--  xml+xslt中检索父ID[推荐]
xml+xslt中检索父ID
作者:ssm1226   出处:www.ssm1226.com   本站发布时间: 点击数:43
这是在CSDN上,帮网友解决的一个问题
其中主要思想是利用命名模板实现递归
xml文件:
<?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='test.xsl'?>
<root>
<a id="3">
<name>aaa</name>
<parentid>6</parentid>
</a>
<a id="1">
<name>bbb</name>
<parentid>3</parentid>
</a>
<a id="6">
<name>cc</name>
<parentid>0</parentid>
</a>
</root>

test.xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxml="urn:schemas-microsoft-com:xslt">
<xsl:template match="/">
<xsl:for-each select="//a">
<xsl:value-of select="name"/>
<xsl:variable name="path">
<xsl:call-template name="pathNode">
<xsl:with-param name="node" select="/root"/>
<xsl:with-param name="parentid" select="parentid"/>
</xsl:call-template>
</xsl:variable>

<xsl:variable name="pathNode" select="msxml:node-set($path)"/>
<xsl:for-each select="$pathNode//parentid">
<xsl:sort select="@sortid" data-type="number" order="descending"/>
<xsl:value-of select="."/>==>
</xsl:for-each>
<xsl:value-of select="@id"/>
<br/>
</xsl:for-each>
</xsl:template>
<xsl:template name="pathNode">
<xsl:param name="node"/>
<xsl:param name="parentid">0</xsl:param>
<xsl:param name="sortid">0</xsl:param>

<parentid sortid="{$sortid}">
<xsl:choose>
<xsl:when test="$parentid != ''"><xsl:value-of select="$parentid"/></xsl:when>
<xsl:otherwise>0</xsl:otherwise>
</xsl:choose>

</parentid>

<xsl:if test="number($parentid) >0">
<xsl:call-template name="pathNode">
<xsl:with-param name="node" select="$node/a[@id > number($parentid)]"/>
<xsl:with-param name="parentid" select="$node/a[@id = $parentid ]/parentid"/>
<xsl:with-param name="sortid" select="number($sortid) + 1"/>
</xsl:call-template>
</xsl:if>

</xsl:template>
</xsl:stylesheet>



--  作者:robert
--  发布时间:5/19/2004 9:33:00 AM

--  
这么多精,想当版主吗?
--  作者:宇宙人
--  发布时间:5/19/2004 9:37:00 AM

--  
you are very clever !
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
46.997ms