Re: [xsl] format string with leading zeros

Subject: Re: [xsl] format string with leading zeros
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Wed, 04 Jan 2012 10:56:20 -0500
At 2012-01-04 15:46 +0000, henry human wrote:
Hi I have following scenario, the itemCode field must be 14 digits lang. I try to ignore the last two decimals of the itemCode field if the itemCode is more than 14 digits long (16 digits). And if the itemCode has less than 14, fill field with leading zeros so that the itemCodeCode is still 14 digits long. Input Example(1): itemCode : 0.0123456789 output itemCode: 000.0123456789 Input Example(2): itemCode : 0.01111123456789 output itemCode: 0.011111234567 Thanks you

Does the following help? It uses two steps to first trim and then pad the input to produce the output.


. . . . . . . . . Ken

~/t/ftemp $ xslt2 henry.xsl henry.xsl
<?xml version="1.0" encoding="UTF-8"?>
000.0123456789
0.011111234567
~/t/ftemp $ cat henry.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  xmlns:my="urn:X-Henry" exclude-result-prefixes="my"
  version="2.0">

<xsl:template match="/">
  <xsl:text>&#xa;</xsl:text>
  <xsl:value-of select="my:trim('0.0123456789')"/>
  <xsl:text>&#xa;</xsl:text>
  <xsl:value-of select="my:trim('0.01111123456789')"/>
  <xsl:text>&#xa;</xsl:text>
</xsl:template>

<xsl:function name="my:trim">
  <xsl:param name="str"/>
  <xsl:variable name="temp"
                select="concat('00000000000000',substring($str,1,14))"/>
  <xsl:sequence select="substring($temp,string-length($temp)-13)"/>
</xsl:function>

</xsl:stylesheet>
~/t/ftemp $


-- Contact us for world-wide XML consulting and instructor-led training Free 5-hour video lecture: XSLT/XPath 1.0 & 2.0 http://ude.my/uoui9h Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/ G. Ken Holman mailto:gkholman@xxxxxxxxxxxxxxxxxxxx Google+ profile: https://plus.google.com/116832879756988317389/about Legal business disclaimers: http://www.CraneSoftwrights.com/legal

Current Thread