xslt中for循环中的错误

时间:2011-08-24 17:00:44

标签: xml xslt xpath

我的xslt文件中有一个for循环,过去一直在工作:

<xsl:template name="for.loop">

<xsl:param name="i"      />
<xsl:param name="count"  />

 <xsl:if test="$i &lt;= $count">
    <colspec colname="{concat('c',$i)}"/>
 </xsl:if>

 <xsl:if test="$i &lt;= $count">
   <xsl:call-template name="for.loop">
      <xsl:with-param name="i">
          <xsl:value-of select="$i + 1"/>
      </xsl:with-param>
      <xsl:with-param name="count">
          <xsl:value-of select="$count"/>
      </xsl:with-param>
  </xsl:call-template>
 </xsl:if>
</xsl:template> 

如您所见,此模板基本上是一个for循环结构,用于创建Cals表模型的“Colspec”节点。我传递给它的参数只是一个索引,它应该是1和一个计数,这意味着应该创建多少个“Colspec”节点。

然后我将此模板称为:

 <xsl:variable name="value">
    <xsl:value-of select="number($colsCount)+number($multiRowCellCount2)"/>                   
  </xsl:variable>                    

<xsl:attribute name="cols">
                   <xsl:value-of select="$value"/>
               </xsl:attribute>   

 <xsl:call-template name="for.loop">
   <xsl:with-param name="i">1</xsl:with-param>
  <xsl:with-param name="count"><xsl:value-of select="$value"/></xsl:with-param>
 </xsl:call-template>

奇怪的是,我达到了“$ value”为9,属性9为“@cols”正确分配的点,但“Colspecs”节点已创建89次!但是,当我尝试另一个文档时,当“$ value”为5时,“@cols”和#of“Colspecs”都是正确的。

我迷失了,为什么当实际计数只有9时,for循环会重复89次?

1 个答案:

答案 0 :(得分:1)

确保在使用它们时将所有数字变量包装在数字函数中,因为我猜你的解析器将它们视为字符串并执行连接(因此9 + 1变为91)...我不知道为什么它只发生在9而不是5。

根据我的评论,代码变为

<xsl:template name="for.loop"> 

  <xsl:param name="i" /> 
  <xsl:param name="count"  /> 

   <xsl:if test="number($i) &lt;= number($count)"> 
     <colspec colname="{concat('c',$i)}"/> 
   </xsl:if> 

   <xsl:if test="number($i) &lt;= number($count)"> 
     <xsl:call-template name="for.loop"> 
       <xsl:with-param name="i"> 
         <xsl:value-of select="number($i) + 1"/> 
       </xsl:with-param> 
       <xsl:with-param name="count"> 
         <xsl:value-of select="$count"/> 
       </xsl:with-param> 
    </xsl:call-template> 
  </xsl:if> 
</xsl:template>  

此外,您连续进行相同的测试,您应该只能删除行

   </xsl:if> 

   <xsl:if test="number($i) &lt;= number($count)">