Hari
Hari

Reputation: 179

Formatting string (Removing leading zeros)

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.

Upvotes: 17

Views: 49986

Answers (8)

Mikro Koder
Mikro Koder

Reputation: 1096

XSLT 2.0 Remove leading zeros from STRING

<xsl:value-of select="replace( $value, '^0+', '')"/>

Upvotes: 4

Henk de Marie
Henk de Marie

Reputation: 77

<xsl:value-of select="number(.) * 1"/>

works for me

Upvotes: 1

Peter Krauss
Peter Krauss

Reputation: 13930

All XSLT1 parser, like the popular libXML2's module for XSLT, have the registered functions facility... So, we can suppose to use it. Suppose also that the language that call XSLT, is PHP: see this wikibook about registerPHPFunctions.


The build-in PHP function ltrim can be used in

  <?xml version="1.0" encoding="UTF-8"?>
  <xsl:stylesheet version="1.0" 
       xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
       xmlns:fn="http://php.net/xsl">
   <xsl:output method="xml" encoding="utf-8" indent="yes"/>
   <xsl:template match="test">
       show <xsl:value-of select="fn:function('ltrim',string(.),'0')" />",
   </xsl:template>
  </xsl:stylesheet>

Now imagine a little bit more complex problem, to ltrim a string with more than 1 number, ex. hello 002 and 021, bye.

The solution is the same: use registerPHPFunctions, except to change the build-in function to a user defined one,

function ltrim0_Multi($s) {
    return preg_replace('/(^0+|(?<= )0+)(?=[1-9])/','',$s);
} 

converts the example into hello 2 and 21, bye.

Upvotes: 0

SebastianBrandt
SebastianBrandt

Reputation: 459

As a simple alternative in XSLT 2.0 that can be used with numeric or alpha-numeric input, with or without leading zeros, you might try:

replace( $value, '^0*(..*)', '$1' )

This works because ^0* is greedy and (..*) captures the rest of the input after the last leading zero. $1 refers to the captured group.

Note that an input containing only zeros will output 0.

Upvotes: 3

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

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(., '#')

Upvotes: 28

Tim C
Tim C

Reputation: 70618

Here is one way you could do it in XSLT 1.0.

First, find the first non-zero element, by removing all the zero elements currently in the value

<xsl:variable name="first" select="substring(translate(., '0', ''), 1, 1)" />

Then, you can find the substring-before this first character, and then use substring-after to get the non-zero part after this

<xsl:value-of select="substring-after(., substring-before(., $first))" />

Or, to combine the two statements into one

<xsl:value-of select="substring-after(., substring-before(., substring(translate(., '0', ''), 1, 1)))" />

So, given the following input

<a>00012095Kb</a>

Then using the following XSLT

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/a">
      <xsl:value-of select="substring-after(., substring-before(., substring(translate(., '0', ''), 1, 1)))" />
   </xsl:template>
</xsl:stylesheet>

The following will be output

12095Kb

Upvotes: 7

Mads Hansen
Mads Hansen

Reputation: 66723

You could use a recursive template that will remove the leading zeros:

<xsl:template name="remove-leading-zeros">
    <xsl:param name="text"/>
    <xsl:choose>
        <xsl:when test="starts-with($text,'0')">
            <xsl:call-template name="remove-leading-zeros">
                <xsl:with-param name="text"
                    select="substring-after($text,'0')"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$text"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

Invoke it like this:

 <xsl:call-template name="remove-leading-zeros">
        <xsl:with-param name="text" select="/path/to/node/with/leading/zeros"/>
    </xsl:call-template>
</xsl:template>

Upvotes: 2

detheridge02
detheridge02

Reputation: 650

There are a couple of ways you can do this. If the value is entirely numeric (for example not a CSV line or part of a product code such as ASN0012345) you can convert from a string to a number and back to a string again :

string(number($value)).

Otherwise just replace the 0's at the start :

replace( $value, '^0*', '' )

The '^' is required (standard regexp syntax) or a value of 001201 will be replaced with 121 (all zero's removed).

Hope that helps. Dave

Upvotes: 9

Related Questions