xsl-list
[Top] [All Lists]

Re: [xsl] format string with leading zeros

2012-01-04 09:56:48
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(_at_)CraneSoftwrights(_dot_)com
Google+ profile: https://plus.google.com/116832879756988317389/about
Legal business disclaimers:    http://www.CraneSoftwrights.com/legal


--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--