c# - 在 XSLT 中迭代

标签 c# xml xslt

我有以下 XSL:

<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl='http://www.w3.org/1999/XSL/Transform' version='1.0'>
  <xsl:param name='width' select ="270"/>
  <xsl:param name='height' select="180"/>
  <xsl:variable name="counter" select="0" />
  <xsl:template name="while">
    <svg xmlns="http://www.w3.org/2000/svg" version="1.1">
      <line x1="{$counter}" y1="0.5" x2="{$counter}" y2="10.5" stroke="black" stroke-width="1" />
      <xsl:variable name="counter" select="$counter + 10" />
      <xsl:if test="$counter &lt; $width">
        <xsl:call-template name="while"/>
      </xsl:if>
    </svg>
  </xsl:template>
</xsl:stylesheet>

我试图在宽度上每 10 个像素绘制一条线,就像标尺标记一样。

当我运行这段代码时,它陷入了循环。我无法调试,我只是得到堆栈溢出异常。我推测要么我的计数器值没有增加 10,要么我对检查计数器 < width 是否不正确的评估。

有人可以给我指出正确的方向吗?

最佳答案

我认为您在调用模板时需要传递参数。

类似于:

<xsl:template name="loop">
    <xsl:param name="count" select="1"/>

    <xsl:if test="$count > 0">
        <xsl:call-template name="loop">
            <xsl:with-param name="count" select="$count - 1"/>
        </xsl:call-template>

        <xsl:value-of select="$count"/>  
        <xsl:text> </xsl:text>

    </xsl:if>    
</xsl:template>

关于c# - 在 XSLT 中迭代,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18363534/

相关文章:

c# - 使用 AppDomain.AssemblyResolve 事件

sql - 查询具有一对多关系的 XML 数据

php - 使用php从xml中的mysql导出数据

python - xslt 模板根标签,在文本输出中省略

xml - XSLT:CSV(或平面文件,或纯文本)到 XML

c# 无需复制和迭代即可将数组类型转换为另一种数组类型

c# - Java 和 C# 之间的 DateTime 转换会引入错误吗?

c# - 在 C# 中从另一个窗体监听主窗体中的事件

java.util.InvalidPropertiesFormatException : SAXParseException

xml - XSLT 1.0 中 Document() 函数的用法