2013-12-11 38 views
0

我想在XSLT中格式化一個數字,使其具有帶前導零的4位數字。XSLT函數format_number不適用於0,前導零的格式爲

E.g.

1234 -> 1234 
12 -> 0012 
1 -> 0001 
0 -> 0000 

爲此我使用的功能format-number如下:

format-number(somevalue, '0000') 

也能正常工作的一切,除了0作爲someValue中。在這裏我得到0而不是0000.

+0

您究竟輸出的結果如何? –

+0

您使用哪種版本的XSLT 2.0處理器? –

+0

不僅版本...哪個XSLT處理器?格式編號(。'0000')使用XSLT和大多數所有處理器返回'0000'...使用Xalan和Saxon 6(1.0)和Saxon HE/PE/EE(2.0)測試 –

回答

0

我有一個代碼片段和一個模板來添加前導零。 也許它可以幫助你:

<IDTNR> 
<xsl:call-template name="checkAndConvertNumeric"> 
    <xsl:with-param name="numValue" select="somevalue"/> 
    <xsl:with-param name="numFormat" select="'0000'"/> 
    </xsl:call-template> 
</IDTNR> 

<xsl:template name="checkAndConvertNumeric"> 
    <xsl:param name="numValue" /> 
    <xsl:param name="numFormat" /> 
    <xsl:choose> 
     <xsl:when test="normalize-space(number($numValue)) = 'NaN'"> 
      <xsl:value-of select="$numValue" /> 
     </xsl:when> 
     <xsl:otherwise> 
      <xsl:value-of select="string(format-number($numValue,$numFormat))" /> 
     </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:with-param name="numFormat" select="'0000'"/> 

您可以指定有多少個零所需要的線。

最好的問候, 彼得

0

我剛剛發現了這個問題。我問題是一個錯誤的檢查數字如下。

<xsl:choose> 
    <xsl:when test="not(number(somevalue))"> 
     <xsl:value-of select="concat(somevalue, ' =&#160;')"/> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:value-of select="concat(format-number(somevalue, '0000'), ' =&#160;')"/> 
    </xsl:otherwise> 
</xsl:choose> 

number(0)得到0而不是(0)成立。因此,0被nt格式化。

我改變了數字檢查,就像Peter在帖子中推薦的那樣,現在它工作得很好。

相關問題