XSL: remove leading zeros

How can I remove leading zeros from variable $test which holds a value such as "008D" or "07C" ? and if no leading zeros then leave as is (i.e 10D).

I have the following:

<xsl:variable name="test" select="anywhere/local"/>

<Request>
      <xsl:value-of select="$test"/>
</Request>
badtz7229Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
i'd think that  translate($test, '0', '') would suffice.
Try that on the three examples that you originally supplied. For the first two examples, it would work fine but for the third example, ie. "10D", you would see the problem. "translate" used in that way removes ALL the zeros from the string, so you'd end up with "1D".

can you please explain it?
I will go through each step, from the innermost function call out. All this explanation assumes an example input of "00010BD0". Also, in each step below, I've substituted the result so far in the appropriate place, hopefully you can follow along with what is happening...


translate($test, '0', '')  -  This call, as touched on above will remove all zeros from the string, so with our example input the result would be '1BD'. I'll explain why this is useful in the next step.

translate($test, '1BD', 'X')  -  This call replaces any '1', 'B', or 'D' characters from the original $test input with 'X', so the result of this step is '000X0XX0'. Hopefully, you can see the big picture of what these two calls are doing, together they are replacing any character that is NOT a '0' with the 'X' character. This is a fairly often used pattern with XSLT, since "translate" can replace any characters that ARE in the supplied set with a replacement, this "double translate" pattern can do the opposite, that is replace any characters that ARE NOT in the supplied set wit a replacement.

substring-before('000X0XX0', 'X')  -  So what we are doing here is really the crux of finding the leading zeros. What this call does is return whatever is in the string "before" the first occurence of 'X', so in this case the result is '000'. Basically, it will return a string containing the leading zeros, if any. If there is no leading zeros, it will return an empty string.

string-length('000')  -  So, here we just get the length of that string of leading zeros. So in this case, the result is 3. If as I mentioned above, there are no leading zeros, the previous step would have resulted in the empty string, which will have a length of zero. So the whole construction up to this point returns basically the count of leading zeros in the original input.

substring($test, 3 + 1)  -  And so the final step is to return the substring from the point AFTER the leading zeros (hence the plus one), so from our original $test string of '00010BD0' we take a substring from the 4th character (as it is 1-based not 0-based) to the end of the string, and the result.... '10BD0'. The leading zeros have been removed but not zeros in the middle or at the end.
0
 
zc2Commented:
A simple recursive template call could delete the leading zeros:
	<xsl:template name="strip-zero">
		<xsl:param name="s"/>
		<xsl:choose>
	     <xsl:when test="starts-with($s,'0')">
			<xsl:call-template name="strip-zero">
				<xsl:with-param name="s" select="substring($s,2)"/>
			</xsl:call-template>
	     </xsl:when>
	     <xsl:otherwise><xsl:value-of select="$s"/></xsl:otherwise>
	  </xsl:choose>
 	</xsl:template>

Open in new window

Call it as following:
<xsl:call-template name="strip-zero">
			<xsl:with-param name="s" select="$test"/>
		</xsl:call-template>

Open in new window

0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
@badtz7229,

What XSLT processor are you using and are you using version 1.0 or version 2.0 in your XSL? Different processor's and different versions support extra functions so this is important to know in order to give you other options for a working solution.

eg. If you are using a version 2 processor, it should support the "replace" function in which case you can do this...
<Request>
      <xsl:value-of select="replace($test, '^0+', '')"/>
</Request>

Open in new window

0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
mccarlConnect With a Mentor IT Business Systems Analyst / Software DeveloperCommented:
Actually, a XSLT v1.0 solution is not that bad. Here is some string manipulation trickery that you could use...
<Request>
      <xsl:value-of select="substring($test, string-length(substring-before(translate($test, translate($test, '0', ''), 'X'), 'X')) + 1)"/>
</Request>

Open in new window

0
 
badtz7229Author Commented:
i am using 1.0
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
i am using 1.0
Did you give the code in my second post a try then?
0
 
badtz7229Author Commented:
@ mccarl

yes your 1.0 solution worked.
can you please explain it?
0
 
badtz7229Author Commented:
why so many translate and substring?
i'd think that  translate($test, '0', '') would suffice.
0
 
badtz7229Author Commented:
Thank u so much. This worked,
0
 
mccarlIT Business Systems Analyst / Software DeveloperCommented:
Glad that I could help! :)
0
All Courses

From novice to tech pro — start learning today.