2017-10-08 75 views
0

我需要實現的幫助,而像xslt中的循環將任何數字轉換爲二進制。在運行此文件時,它在瀏覽器上顯示以下錯誤:如何實現while循環在xslt中

XSLT轉換期間出錯:發生未知錯誤()。

只是尋找一點邏輯來指導我。

下面是XML文件:

<?xml version="1.0" encoding="UTF-8"?> 
<?xml-stylesheet type="text/xsl" href="temp.xsl"?> 
<catalog> 
<data>2</data> 
</catalog> 

下面是XSLT文件,它似乎有問題,呼籲 「循環」 功能:

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
version="1.0"> 
<xsl:template match="/"> 
    <xsl:call-template name ="loop" > 
      <xsl:with-param name="var" select="catalog/data"/> 
      <xsl:with-param name="temp" select= "$var div 2"/> 
      <xsl:with-param name="data" select= "$var mod 2"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="loop"> 
<xsl:param name="var"></xsl:param> 
<xsl:param name="temp"></xsl:param> 
<xsl:param name="data"></xsl:param> 
<xsl:choose> 
<xsl:when test= "$temp &gt; 0"> 
    <xsl:value-of select="$data"/> 
    <xsl:call-template name="loop"> 
     <xsl:with-param name="var"/> 
     <xsl:with-param name="data" select="$data mod 2"/> 
     <xsl:with-param name="temp" select="$temp div 2"/> 
    </xsl:call-template> 
</xsl:when> 
<xsl:otherwise> 
<xsl:value-of select= "$data"/> 
</xsl:otherwise> 
</xsl:choose> 
</xsl:template> 
</xsl:stylesheet> 

回答

0

DIV沒有整數除法 - 讓你獲得無限遞歸。 添加地板

而且 - 在我看來,無功不使用循環內

而且<xsl:value-of select= "$data"/>存在於兩個分支 - 因此它可以在境外進行移動和選擇可能被更改爲如果

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:template match="/"> 
     <xsl:variable name="var"><xsl:value-of select="catalog/data" /></xsl:variable> 
     <xsl:call-template name="loop"> 
     <xsl:with-param name="temp" select="floor($var div 2)" /> 
     <xsl:with-param name="data" select="$var mod 2" /> 
     </xsl:call-template> 
    </xsl:template> 
    <xsl:template name="loop"> 
     <xsl:param name="temp" /> 
     <xsl:param name="data" /> 
     <xsl:value-of select="$data" /> 
     <xsl:if test="$temp &gt; 0"> 
     <xsl:call-template name="loop"> 
      <xsl:with-param name="temp" select="floor($temp div 2)" /> 
      <xsl:with-param name="data" select="$data mod 2" /> 
     </xsl:call-template> 
     </xsl:if> 
    </xsl:template> 
</xsl:stylesheet>