2012-10-16 26 views
2

我正在使用javascript填充我的xslt文件中的數字。但在調試時出現錯誤。Javascript與xlt格式錯誤

不支持具有Clr類型 'ConcatString'的擴展函數參數或返回值。

我該如何解決這個問題?

XSLT

<xsl:template name="padNumber"> 
    <xsl:param name="value"></xsl:param> 
    <xsl:param name="length"></xsl:param> 
    <xsl:value-of select="user:PadDigits($value,$length)"/> 
    </xsl:template> 

的JavaScript

function PadDigits(n, totalDigits) 
{ 
    n = n.toString(); 
    var pd = ''; 
    if (totalDigits &gt; n.length) 
    { 
     var i; 
     for (i=0; i&lt;(totalDigits-n.length); i++) 
     { 
      pd += '0'; 
     } 
    } 
    pd = pd + n.toString(); 
    return pd; 
} 
+0

不是一個答案,但你可以使用'號格式做到這一點不帶擴展功能($值,子(「0000000000000000000000」,1,$長度))'(如果你需要添加更多的零) –

回答

2

你不需要的擴展功能這個根本。

使用這種純XSLT實現

<xsl:template name="padNumber"> 
    <xsl:param name="pValue"/> 
    <xsl:param name="pLength"/> 

    <xsl:value-of select= 
    "concat(substring(substring($vZeroes,1,$pLength), 
         string-length($pValue) +1), 
      $pValue) 
    "/> 
    </xsl:template> 

下面是一個完整的例子

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="vZeroes" select= 
    "'000000000000000000000000000000000000000'"/> 

<xsl:template match="/"> 
    <xsl:call-template name="padNumber"> 
    <xsl:with-param name="pValue" select="12345"/> 
    <xsl:with-param name="pLength" select="8"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="padNumber"> 
    <xsl:param name="pValue"/> 
    <xsl:param name="pLength"/> 

    <xsl:value-of select= 
    "concat(substring(substring($vZeroes,1,$pLength), 
         string-length($pValue) +1), 
      $pValue) 
    "/> 
    </xsl:template> 
</xsl:stylesheet> 

當這種轉變是在任何XML文檔應用(在該演示中使用),產生想要的,正確的結果

00

您可以進一步參數要使用的所需填充字符:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="text"/> 

<xsl:variable name="vZeroes" select= 
    "'000000000000000000000000000000000000000'"/> 

<xsl:template match="/"> 
    <xsl:call-template name="padNumber"> 
    <xsl:with-param name="pValue" select="12345"/> 
    <xsl:with-param name="pLength" select="8"/> 
    <xsl:with-param name="pPadChar" select="'*'"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="padNumber"> 
    <xsl:param name="pValue"/> 
    <xsl:param name="pLength"/> 
    <xsl:param name="pPadChar" select="'0'"/> 

    <xsl:variable name="vZeroes" select="translate($vZeroes, '0', $pPadChar)"/> 

    <xsl:value-of select= 
    "concat(substring(substring($vZeroes,1,$pLength), 
         string-length($pValue) +1), 
      $pValue) 
    "/> 
    </xsl:template> 
</xsl:stylesheet> 

當進行這種轉變,其結果是現在:

***12345 
3

更改PadDigits返回字符串:

return "" + pd; 
0

@RoManiac感謝幫助。我發現這個模板,我更喜歡任何人來看看,稍後看。

<xsl:template name="format-batchnum"> 
    <xsl:param name="batchnum"/> 
    <xsl:param name="numbatchdigit" select="12"/> 

    <xsl:choose> 
     <xsl:when test="string-length($batchnum)>=$numbatchdigit"> 
     <xsl:value-of select="$batchnum"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:text>0</xsl:text> 
     <xsl:call-template name="format-batchnum"> 
      <xsl:with-param name="batchnum" select="$batchnum"/> 
      <xsl:with-param name="numbatchdigit" select="$numbatchdigit -1"/> 
     </xsl:call-template> 
     </xsl:otherwise> 
    </xsl:choose> 
    </xsl:template> 
+1

如果有幫助,請將其標記爲答案。 – sainiuc