Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting string (Removing leading zeros)

Tags:

xslt

xslt-1.0

I am newbie to xslt. My requirement is to transform xml file into text file as per the business specifications. I am facing an issue with one of the string formatting issue. Please help me out if you have any idea.

Here is the part of input xml data: "0001295"

Expected result to print into text file: 1295

My main issue is to remove leading Zeros. Please share if you have any logic/function.

like image 472
Hari Avatar asked Jan 10 '12 09:01

Hari


People also ask

How do I remove leading zeros from a string in Excel?

Multiplying the Column with 1 or Adding 0 Multiplying the leading zeros values with 1 or adding 0 to them will remove the leading zeros. The mechanics of this is to subject the number with leading zeros to a calculation that will not change its original value.

How do you remove leading zeros from a string in C++?

Using Stoi() Method stoi() function in C++ is used to convert the given string into an integer value. It takes a string as an argument and returns its value in integer form. We can simply use this method to convert our string to an integer value which will remove the leading zeros.

How do you remove leading zeros from a string in Java 8?

The replaceAll() method of the String class accepts two strings representing a regular expression and a replacement String and replaces the matched values with given String. The ^0+(?! $)"; To remove the leading zeros from a string pass this as first parameter and “” as second parameter.


1 Answers

Just use this simple expression:

number(.)

Here is a complete example:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="text"/>

 <xsl:template match="t">
      <xsl:value-of select="number(.)"/>
 </xsl:template>
</xsl:stylesheet>

When applied on this XML document:

<t>0001295</t>

the wanted, correct result is produced:

1295

II. Use format-number()

format-number(., '#')
like image 158
Dimitre Novatchev Avatar answered Sep 29 '22 12:09

Dimitre Novatchev