|
查找公共祖先节点
|
|
| |
|
来源:XSLT.org.cn 作者:XSLT专业站 发布时间:2007-08-04
|
|
查找公共祖先节点
题目:
<root> <item id="1" parent="0" /> <item id="2" parent="1" /> <item id="3" parent="2" /> <item id="4" parent="1" /> </root>
查找公共祖先结点
例如得到一个<root id1='4' id2='3' />的XML文件,以及一个 目标示例(Filename:tree.xml).
输出的结果为:1.
实现:
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/> <xsl:key name="searchid" match="item" use="@id"/> <xsl:template match="/"> <xsl:call-template name="same-ancestor"> <xsl:with-param name="id1" select="'3'"/> <xsl:with-param name="id2" select="'4'"/> </xsl:call-template> </xsl:template> <xsl:template name="same-ancestor"> <xsl:param name="id1" select="'0'"/> <xsl:param name="id2" select="'0'"/> <xsl:variable name="setstr"> <xsl:for-each select="key('searchid',$id1)/ancestor-or-self::*"> <xsl:variable name="tmpid" select="generate-id(.)"/> <xsl:if test="not(key('searchid',$id2)/ancestor-or-self::*[generate-id(.)=$tmpid])"> <xsl:value-of select="../@id"/>-- </xsl:if> </xsl:for-each> </xsl:variable> <xsl:choose> <xsl:when test="$setstr != ''"> <xsl:value-of select="key('searchid',substring-before($setstr,'--'))/@id"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="$id1"/> </xsl:otherwise> </xsl:choose> </xsl:template> </xsl:stylesheet>
标题: 查找公共祖先节点
关键字:公共祖先节点 Node substring-before ancestor-or-self
|
| 上一篇:查找节点的父节点 下一篇:用 XML/XSL 来生成动态页面 |
|
|
|
|
|