2

I got an input like 00000BE0891.116.828. I have to remove dots and leading zeroes from it. I tried with the translate() function which removed dots from it.

Then I tried string(number(00000BE0891.116.828)), but it returned NaN, because number() function doesn't validate alphabets. Does anyone have any suggestions?

Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66
Kundan
  • 153
  • 1
  • 5
  • 15

2 Answers2

6

One approach could be to use translate to remove all zeroes, which will then tell you what the first non-zero character is. You can then use substring-after to chop off the leading zeroes in this way.

<xsl:variable name="firstNonZero" 
              select="substring(translate($number, '0', ''), 1, 1)" />
<xsl:variable name="noLeadingZeroes"
              select="concat($firstNonZero, substring-after($number, $firstNonZero))" />
<xsl:value-of select="translate($noLeadingZeroes, '.', '')" />

(Where $number is your starting input "00000BE0891.116.828")

Or, if you wanted to combine this into one expression...

<xsl:value-of 
    select="translate(concat(substring(translate($number, '0', ''), 1, 1), substring-after($number, substring(translate($number, '0', ''), 1, 1))), '.', '')" />
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • `translate($number, '^0*', '')` would be easier to write – Mark Veenstra Nov 08 '13 at 09:58
  • Regular expressions are not available in XLST 1.0 unfortunately. (In XSLT 2.0, it would be the `replace` function that can use regular expressions. I think `translate` only ever takes a plain string as an argument). – Tim C Nov 08 '13 at 10:01
  • Tested it in XSLT 1.0 as shown below and `translate()` can handle regular expressions. See also the second answer here: http://stackoverflow.com/questions/8801289/formatting-string-removing-leading-zeros – Mark Veenstra Nov 08 '13 at 12:16
  • Using translate, in either XSLT 1.0 or XSLT 2.0 returns "BE891116828". All zeroes have been removed, not just the leading zeroes. Also, see the W3C spec at http://www.w3.org/TR/2010/REC-xpath-functions-20101214/#func-translate where there is no mention of regular expressions. `replace` would be the way to go in XSLT2.0 though. – Tim C Nov 08 '13 at 13:56
0

You can also use translate() to remove leading zeros from an alphanumeric value. For example if you have an XML like this:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <value>00000BE0891.116.828</value>
</data>

You could use a XSLT like this:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()" />
        </xsl:copy>
    </xsl:template>

    <xsl:template match="value">
        <xsl:copy>
            <xsl:value-of select="translate(., '^0*', '' )" />
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

^0* is a regular expression and will remove all leading zeros. If you want to combine with the translate of the dot, you can do this:

<xsl:value-of select="translate(translate(., '^0*', ''), '.', '')" />
Mark Veenstra
  • 4,691
  • 6
  • 35
  • 66