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:34

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>
查看更多
对你真心纯属浪费
3楼-- · 2019-02-16 10:34
<xsl:value-of select="number(.) * 1"/>

works for me

查看更多
登录 后发表回答