如何循環一組節點,其中節點名稱具有數字編號,並且數字按照系列遞增?XSL循環:如何在節點名稱增量處循環
例如:
<nodes>
<node1>
<node2>
...
<node10>
</nodes>
如何循環一組節點,其中節點名稱具有數字編號,並且數字按照系列遞增?XSL循環:如何在節點名稱增量處循環
例如:
<nodes>
<node1>
<node2>
...
<node10>
</nodes>
遞歸命名模板可以這樣做:
<xsl:template name="processNode">
<xsl:param name="current" select="1"/>
<xsl:variable name="currentNode" select="*[local-name() = concat('node', $current)]"/>
<xsl:if test="$currentNode">
<!-- Process me -->
<xsl:call-template name="processNode">
<xsl:with-param name="current" select="$current + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
或者,如果你不關心順序,只是一個普通的模板:
<xsl:template match="*[starts-with(local-name(), 'node')]">
</xsl:template>
除非我完全錯過了某些東西,否則你需要的是如此簡單。
<xsl:template match="nodes">
<xsl:for-each select="*">
<!-- Do what you want with each node. -->
</xsl:for-each>
</xsl:template>
KISS ... +1更簡單:
<xsl:template match="nodes">
<xsl:apply-templates select="*">
<!-- the xsl:sort is redundant if the input already is in correct order -->
<xsl:sort select="substring-after(name(), 'node')" data-type="number" />
</xsl:apply-templates>
</xsl:template>
<xsl:template match="nodes/*">
<!-- whatever -->
</xsl:template>
的 「遞歸命名模板」 是太複雜了。完全沒有遞歸和更短的代碼也可以達到同樣的效果。 ;-) – Tomalak 2010-01-20 10:48:00