【发布时间】:2010-01-19 20:08:51
【问题描述】:
如何循环通过一组节点,其中节点名称有一个数字编号,并且数字按系列递增?
例如:
<nodes>
<node1>
<node2>
...
<node10>
</nodes>
【问题讨论】:
标签: xslt loops while-loop
如何循环通过一组节点,其中节点名称有一个数字编号,并且数字按系列递增?
例如:
<nodes>
<node1>
<node2>
...
<node10>
</nodes>
【问题讨论】:
标签: xslt loops while-loop
除非我完全遗漏了某些东西,否则您需要的就是这么简单。
<xsl:template match="nodes">
<xsl:for-each select="*">
<!-- Do what you want with each node. -->
</xsl:for-each>
</xsl:template>
【讨论】:
递归命名模板可以做到这一点:
<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: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>
【讨论】: