我理解如何使用XSLT从最外层的person元素到最里层的元素处理这个doc(下面).但我想知道是否: 如果它可以从最深的元素出来工作. 根据我的例子,这会是什么样子. ?xml version="1.0" encoding="
>如果它可以从最深的元素出来工作.
>根据我的例子,这会是什么样子.
<?xml version="1.0" encoding="utf-8" ?>
<container>
<person name="Larry">
<person name="Moe">
<person name="Curly">
<person name="Shemp">
</person>
</person>
</person>
</person>
</container>
以下是执行“向后递归”的最常用方法,该方法不依赖于此特定问题,可用于各种各样的问题.
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:call-template name="backwardsRecursion">
<xsl:with-param name="pList" select="//person"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="backwardsRecursion">
<xsl:param name="pList"/>
<xsl:if test="$pList">
<xsl:apply-templates select="$pList[last()]"/>
<xsl:call-template name="backwardsRecursion">
<xsl:with-param name="pList" select=
"$pList[position() < last()]"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template match="person">
<xsl:value-of select="concat(@name,'
')"/>
</xsl:template>
</xsl:stylesheet>
当应用于最初提供的XML文档时,产生想要的结果:
Shemp Curly Moe Larry
请注意,调用名为“backwardsRecursion”的通用模板,它实际上实现了向后递归.此模板不了解它处理的节点或它们将如何处理.
因此,该模板可用于需要向后递归处理的每种情况.
