Feargal Hogan
Feargal Hogan

Reputation: 191

Problems formatting trailing zeroes to specific decimal places

I'm having some difficulty with the format-number function.

Given this XML

<testvalues>
    <test value="02.25"/>
    <test value="02.2"/>
    <test value="02.20"/>
</testvalues>

I am trying to produce this output

<testvalues>
    <test>02.25</test>
    <test>02.2</test>
    <test>02.20</test>
</testvalues>

But I can't find a picture-string do do that.

Given this xslt

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>
    
    <xsl:template match="testvalues">
        <xsl:copy>
            <xsl:apply-templates mode="by-1d"/>
            <xsl:apply-templates mode="by-1plus1d"/>
            <xsl:apply-templates mode="by-2d"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="test" mode="by-1d">
        <xsl:copy>
            <xsl:value-of select="format-number(@value,'00.0')"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="test" mode="by-1plus1d">
        <xsl:copy>
            <xsl:value-of select="format-number(@value,'00.0#')"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="test" mode="by-2d">
        <xsl:copy>
            <xsl:value-of select="format-number(@value,'00.00')"/>
        </xsl:copy>
    </xsl:template>
    
</xsl:stylesheet>

the output created is

<?xml version="1.0" encoding="UTF-8"?>
<testvalues>
    <test>02.2</test>
    <test>02.2</test>
    <test>02.2</test>

    <test>02.25</test>
    <test>02.2</test>
    <test>02.2</test>

    <test>02.25</test>
    <test>02.20</test>
    <test>02.20</test>
</testvalues>

The trailing zeros do not match the the incoming digits.

I want to print just 1 trailing digit where the input is 1 trailing digit, but 2 digits (including zero) where the input has >1 trailing digit.

Is that possible?

Upvotes: 0

Views: 337

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 116959

I want to print just 1 trailing digit where the input is 1 trailing digit, but 2 digits (including zero) where the input has >1 trailing digit.

To do exactly that, you could use:

<xsl:template match="test">
    <xsl:copy>
        <xsl:value-of select="format-number(@value, if(string-length(substring-after(@value, '.')) > 1) then'00.00' else '00.0')"/>
    </xsl:copy>
</xsl:template>

Given the input of:

XML

<testvalues>
    <test value="02"/>
    <test value="02.2"/>
    <test value="02.20"/>
    <test value="02.200"/>
    <test value="02.2000"/>
    <test value="02.20002"/>
</testvalues>

this would produce:

Result

<?xml version="1.0" encoding="UTF-8"?>
<testvalues>
   <test>02.0</test>
   <test>02.2</test>
   <test>02.20</test>
   <test>02.20</test>
   <test>02.20</test>
   <test>02.20</test>
</testvalues>

Demo: https://xsltfiddle.liberty-development.net/nbBfrFy/2

Upvotes: 1

Related Questions