简体   繁体   中英

format-number () in xslt - constant decimal part

I am just starting with xslt and looking to transform a value "0000011287000" into "1,1287000". So, basically last 7 digits are decimals with decimal separator ','.

If the value is '0000007748300', it must return '0,7748300'.

I tried using format-number() in xslt 2.0, it works for the former case but not for the latter one, as expected, as it returns '7748300'.

format-number($value,'#,#######')

My next idea is to split the number and concatenate with decimal separator which is likely a ugly way. So, trying to see if there is something which could be done using format-number() itself.

I would multiply the input by 1E-7 and then use format-number:

<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">

    <xsl:decimal-format decimal-separator="," grouping-separator="."/>

    <xsl:template match="data">
        <xsl:copy>
            <xsl:value-of select="format-number(. * 1E-7, '0,0000000')"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
</xsl:transform>

transforms

<root>
    <data>0000011287000</data>
    <data>0000007748300</data>
</root>

into

<root>
    <data>1,1287000</data>
    <data>0,7748300</data>
</root>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM