使用xslt显示基于属性值的列数

时间:2013-08-05 09:16:43

标签: xslt

我想显示< td>基于计数的属性值的列,使用 for-each 循环。有人可以帮我实现吗?我是XSLT开发的新手。

XML节点:
< row count =“72”>

编辑:计数 - 此处引用行中的列数

提前致谢。

2 个答案:

答案 0 :(得分:1)

如果你的计数不是数千,这可以通过简单的递归来完成。迭代是不可能的,因为XSLT中没有可修改的变量。

<xsl:template match="row[@count &gt; 0]">
  <xsl:call-template name="new-td">
    <xsl:with-param name="count" select="@count" />
  </xsl:call-template>
</xsl:template>

<xsl:template name="new-td">
  <xsl:param name="count" select="0" />

  <xsl:if test="$count">
    <!-- create current cell -->
    <td>
      <!-- contents... -->
    </td>

    <!-- recursive step: create next cell -->
    <xsl:call-template name="new-td">
      <xsl:with-param name="count" select="$count - 1" />
    </xsl:call-template>
  </xsl:if>
</xsl:template>

根据XSLT处理器的智能程度,它会将其优化为幕后迭代循环,这样就不会发生堆栈溢出错误,无论$count有多高。尝试使用非常高的数字来了解XSLT处理器的反应。

IBM DeveloperWorks推荐阅读:Use recursion effectively in XSL,尤其是“递归示例2:迭代数字

部分。

答案 1 :(得分:1)

在XSLT 2.0中,您可以使用“to”运算符,只需说出

即可
<xsl:template match="row">
  <xsl:variable name="theRow" select="." />
  <tr>
    <xsl:for-each select="1 to @count">
      <td><!-- insert cell contents here --></td>
    </xsl:for-each>
  </tr>
</xsl:template>

for-each中,.将是当前的,因此如果您需要访问row元素,则需要将其保存在变量中

相关问题