Solved

XSL: remove leading zeros

Posted on 2014-04-15
10
954 Views
Last Modified: 2014-04-22
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>
0
Comment
Question by:badtz7229
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 18

Expert Comment

by:zc2
ID: 40002649
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
 
LVL 35

Expert Comment

by:mccarl
ID: 40002951
@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
 
LVL 35

Assisted Solution

by:mccarl
mccarl earned 500 total points
ID: 40002958
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:badtz7229
ID: 40002962
i am using 1.0
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40002966
i am using 1.0
Did you give the code in my second post a try then?
0
 

Author Comment

by:badtz7229
ID: 40004567
@ mccarl

yes your 1.0 solution worked.
can you please explain it?
0
 

Author Comment

by:badtz7229
ID: 40004582
why so many translate and substring?
i'd think that  translate($test, '0', '') would suffice.
0
 
LVL 35

Accepted Solution

by:
mccarl earned 500 total points
ID: 40005564
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
 

Author Closing Comment

by:badtz7229
ID: 40009291
Thank u so much. This worked,
0
 
LVL 35

Expert Comment

by:mccarl
ID: 40016475
Glad that I could help! :)
0

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing CSS, there are a few simple rules that will make your life easier.    1. Using ‘* {box-sizing:border-box;}’. Using this will wrap all your elements in a nice little compact box-model that will give you the width you want, like so... …
Building a website can seem like a daunting task to the uninitiated but it really only requires knowledge of two basic languages: HTML and CSS.
In this tutorial viewers will learn how to style elements, such a divs, with a "drop shadow" effect using the CSS box-shadow property Start with a normal styled element, such as a div.: In the element's style, type the box shadow property: "box-shad…
In this tutorial viewers will learn how to style rounded corners for elements in CSS using the border-radius property Begin with a normal styled element such as a div: To style all four corners of the div to be the same degree of roundness, use the …

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question