2012-10-18 55 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”相关联的脚本或扩展对象。它来自,任何想法为什么?谢谢 –