【问题标题】:XSL Looping: how to loop where node name incrementsXSL 循环:如何在节点名称递增的地方循环
【发布时间】:2010-01-19 20:08:51
【问题描述】:

如何循环通过一组节点,其中节点名称有一个数字编号,并且数字按系列递增?

例如:

<nodes>
  <node1>
  <node2>
  ...
  <node10>
</nodes>

【问题讨论】:

    标签: xslt loops while-loop


    【解决方案1】:

    除非我完全遗漏了某些东西,否则您需要的就是这么简单。

    <xsl:template match="nodes">
        <xsl:for-each select="*">
            <!-- Do what you want with each node. -->
        </xsl:for-each>
    </xsl:template>
    

    【讨论】:

    • +1 for KISS ... 更简单:
    【解决方案2】:

    递归命名模板可以做到这一点:

    <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>
    

    【讨论】:

    • “递归命名模板”太复杂了。完全不需要递归和更短的代码也可以达到同样的效果。 ;-)
    【解决方案3】:
    <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>
    

    【讨论】:

      猜你喜欢
      • 2010-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      相关资源
      最近更新 更多