Pad Value with fix length - xslt

5.5k Views Asked by At

I have this simple xslt code:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text" indent="yes"/>
   <xsl:template match="/racine/Requete">
        <xsl:for-each select="Row">
            <xsl:value-of select="Col[@name = 'Service Point']/." />
            <xsl:text>ME02</xsl:text>
            <xsl:value-of select="Col[@name = 'Meter Number']/." />
            <xsl:value-of select="Col[@name = 'Date']/." />
            <xsl:value-of select="translate(Col[@name = 'Import Active kWh']/.,',','.')" />
            <xsl:text>30000000000000000000</xsl:text>
            <xsl:text>&#13;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The Meter Number parameter can have max 10 alphanumeric. so if for example the value is 232345, I want to display (pad with spaces):' 232345'. I have the same case for Import Active kWh, this is a numeric field fix 12 type numeric, but if the value is 56884, I want to display (pad with 0 at the begining) '000000056884'.

Thanks for your useful help as usual!

Regards.

2

There are 2 best solutions below

0
On BEST ANSWER

For the numeric field you can simply use the format-number() function, e.g.:

<xsl:value-of select="format-number($numeric-field, '000000000000')"/>

For the alpha-numeric field, try:

<xsl:value-of select="substring(concat('          ', $alphanumeric-field), 1 + string-length($alphanumeric-field))" />

If you need to do it more than once, consider re-writing this as a function.


Note: this location step /. doesn't do anything.

0
On

XSLT has format-number function so format-number(translate(Col[@name = 'Import Active kWh']/.,',','.'), '000000000000') should solve one problem.