Formatting string (Removing leading zeros)

后端 未结 8 2036
鱼传尺愫
鱼传尺愫 2021-01-11 10:07

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.

相关标签:
8条回答
  • 2021-01-11 11:08

    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(., '#')
    
    0 讨论(0)
  • 2021-01-11 11:11

    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

    0 讨论(0)
提交回复
热议问题