Formatting string (Removing leading zeros)

2019-02-16 09:36发布

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.

标签: xslt xslt-1.0
8条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-16 10:13

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
查看更多
狗以群分
3楼-- · 2019-02-16 10:16

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.

查看更多
兄弟一词,经得起流年.
4楼-- · 2019-02-16 10:27

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.

查看更多
放我归山
5楼-- · 2019-02-16 10:31

XSLT 2.0 Remove leading zeros from STRING

<xsl:value-of select="replace( $value, '^0+', '')"/>
查看更多
我欲成王,谁敢阻挡
6楼-- · 2019-02-16 10:32

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(., '#')
查看更多
可以哭但决不认输i
7楼-- · 2019-02-16 10:33

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

查看更多
登录 后发表回答