?
Solved

XSL: remove leading zeros

Posted on 2014-04-15
10
Medium Priority
?
1,044 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 36

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 36

Assisted Solution

by:mccarl
mccarl earned 2000 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
Technology Partners: 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 36

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 36

Accepted Solution

by:
mccarl earned 2000 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 36

Expert Comment

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

Featured Post

CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
In this Micro Tutorial users will learn how to embed custom fonts into websites using @font-face in CSS Select a font: Ensure the EULA allows you to use @font-face: Download the font: Get the browser-compatible files you need: Edit your CSS       - Name …
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…
Suggested Courses
Course of the Month14 days, 2 hours left to enroll

801 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