类似于师德的解决方案,但让“人”的要素是在没有直接父子关系如下:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="//person[not(person)]"/>
</xsl:template>
<xsl:template match="person" name="tPerson">
<xsl:value-of select="concat(@name,'
')"/>
<xsl:apply-templates select=
"ancestor::person[1]"/>
</xsl:template>
</xsl:stylesheet>
当在最初提供的XML文档施加时,正确的结果产生:
Shemp
卷毛
萌
拉里
这是好事,知道这种类型的许多问题并不需要递归了! 这种转变产生完全相同的结果,只是涉及迭代:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:for-each select="//person[not(person)]">
<xsl:for-each select="ancestor::person | .">
<xsl:sort select="position()" order="descending"/>
<xsl:value-of select="concat(@name,'
')"/>
</xsl:for-each>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
请注意,确保处理的相反顺序排序。
@Bruno Tyndall我已经用.NET 2.0 + XslCompiledTransform测试了这个转换。它应该也适用于较旧的XslTransform。 – 2009-02-12 03:30:19