2012-10-18 53 views
0

我需要在xslt格式化小數值。該模板應該刪除小數。並顯示指定的數字後跟指定的小數位數。圓形模板圍繞小數點。當我嘗試使用我當前的模板時,我得到了NaN。xslt格式編號

模板應該轉變992.45到000000992如果digitCount = 9

<xsl:template name="RoundedDecimalFormat"> 
    <xsl:param name="num"></xsl:param> 
    <xsl:param name="digitCount"></xsl:param> 
    <xsl:variable name="value" select="format-number(round($num), '#########')"></xsl:variable> 
    <xsl:call-template name="format-batchnum"> 
     <xsl:with-param name="batchnum" select="$value"></xsl:with-param> 
     <xsl:with-param name="numbatchdigit" select="$digitCount"></xsl:with-param> 
    </xsl:call-template> 
    </xsl:template> 

下面模板應轉換1323.91至00000132391,如果manyDigits = 9

<xsl:template name="UnRoundedDecimalFormat"> 
    <xsl:param name="time"></xsl:param> 
    <xsl:param name="manyDigits"></xsl:param> 
    <xsl:variable name="value" select="format-number($time, '#########')"></xsl:variable> 
    <xsl:call-template name="format-batchnum"> 
     <xsl:with-param name="batchnum" select="$value"></xsl:with-param> 
     <xsl:with-param name="numbatchdigit" select="$manyDigits"></xsl:with-param> 
    </xsl:call-template> 
    </xsl:template> 

這部分工作正常,我在其他地方使用

<xsl:template name="format-batchnum"> 
    <xsl:param name="batchnum"/> 
    <xsl:param name="numbatchdigit"/> 
    <xsl:value-of select="concat(substring(substring($vZeroes,1,$numbatchdigit), string-length($batchnum) +1), $batchnum)"/> 
    </xsl:template> 

修訂版 *

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
       xmlns:fo="http://www.w3.org/1999/XSL/Format" 
       xmlns:msxsl="urn:schemas-microsoft-com:xslt" 
       xmlns:ns0="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:fn="http://www.w3.org/2005/02/xpath-functions " 
       xmlns:xs="http://www.w3.org/2001/XMLSchema" 
       version="2.0"> 


<xsl:call-template name="UnRoundedDecimalFormat"> 
     <xsl:with-param name="salary" select="$annualSalary"></xsl:with-param> 
    </xsl:call-template> 


<xsl:template name="RoundedDecimalFormat"> 
    <xsl:param name="num"></xsl:param> 
    <xsl:param name="digitCount" select ="9"></xsl:param> 
    <xsl:variable name="value" select="format-number(round($num), '#########')"></xsl:variable> 
    <xsl:call-template name="format-batchnum"> 
     <xsl:with-param name="batchnum" select="$value"></xsl:with-param> 
     <xsl:with-param name="numbatchdigit" select="$digitCount"></xsl:with-param> 
    </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="UnRoundedDecimalFormat"> 
    <xsl:param name="time"></xsl:param> 
    <xsl:param name="manyDigits" select ="9"></xsl:param> 

    <xsl:variable name="vTime" select="translate(string($time), '.', '')"/> 

    <xsl:variable name="value" select="format-number(number(xs:string($vTime)), '#########')"></xsl:variable> 
    <xsl:call-template name="format-batchnum"> 
     <xsl:with-param name="batchnum" select="$value"></xsl:with-param> 
     <xsl:with-param name="numbatchdigit" select="$manyDigits"></xsl:with-param> 
    </xsl:call-template> 
</xsl:template> 


function roundedDecimalFormat(number){ 
try{ 
var n = Math.round(number); 
return n;} 
catch(err){return '';} 
} 

function removeDecimal(number){ 
try{ 
var n = number.replace('.',''); 
return n;} 
catch(err){ 
return ''; 
} 
}; 

回答

1

只是輕微碰觸到您的代碼根據需要它的工作:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xsl:output omit-xml-declaration="yes" indent="yes"/> 

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

<xsl:template match="/"> 
    <xsl:call-template name="RoundedDecimalFormat"> 
     <xsl:with-param name="num" select="992.45"/> 
     <xsl:with-param name="digitCount" select="9"/> 
    </xsl:call-template> 
========================= 
    <xsl:call-template name="UnRoundedDecimalFormat"> 
     <xsl:with-param name="time" select="1323.91"/> 
     <xsl:with-param name="manyDigits" select="9"/> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="RoundedDecimalFormat"> 
    <xsl:param name="num"></xsl:param> 
    <xsl:param name="digitCount"></xsl:param> 
    <xsl:variable name="value" select="format-number(round($num), '#########')"></xsl:variable> 
    <xsl:call-template name="format-batchnum"> 
     <xsl:with-param name="batchnum" select="$value"></xsl:with-param> 
     <xsl:with-param name="numbatchdigit" select="$digitCount"></xsl:with-param> 
    </xsl:call-template> 
    </xsl:template> 

<xsl:template name="UnRoundedDecimalFormat"> 
    <xsl:param name="time"></xsl:param> 
    <xsl:param name="manyDigits"></xsl:param> 

    <xsl:variable name="vTime" select="translate(string($time), '.', '')"/> 

    <xsl:variable name="value" select="format-number(number(xs:string($vTime)), '#########')"></xsl:variable> 
    <xsl:call-template name="format-batchnum"> 
     <xsl:with-param name="batchnum" select="$value"></xsl:with-param> 
     <xsl:with-param name="numbatchdigit" select="$manyDigits"></xsl:with-param> 
    </xsl:call-template> 
</xsl:template> 

<xsl:template name="format-batchnum"> 
    <xsl:param name="batchnum"/> 
    <xsl:param name="numbatchdigit"/> 
    <xsl:value-of select="concat(substring(substring($vZeroes,1,$numbatchdigit), string-length($batchnum) +1), $batchnum)"/> 
</xsl:template> 
</xsl:stylesheet> 

當這種轉變是在任何XML文檔(未使用)應用,通緝,會產生正確的結果

000000992 
========================= 
000132391 
+0

謝謝我會給我一個鏡頭,當我得到t o辦公室明天! –

+0

@Rufio,不客氣。 –

+0

嘗試調試時出現此錯誤。無法找到與命名空間「http://www.w3.org/2001/XMLSchema」相關聯的腳本或擴展對象。它來自,任何想法爲什麼?謝謝 –