Solved

XSL: remove leading zeros

Posted on 2014-04-15
10
846 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
  • 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
 

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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
In this tutorial viewers will learn how to style a corner ribbon overlay for an image using CSS Create a new class by typing ".Ribbon":  Define the class' "display:" as "inline-block": Define its "position:" as "relative": Define its "overflow:" as …
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…

757 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now