2

我得到了一个像00000BE0891.116.828. 我必须从中删除点和前导零。我尝试使用translate()从中删除点的功能。

然后我尝试string(number(00000BE0891.116.828))了,但它返回了NaN,因为number()函数不验证字母。有没有人有什么建议?

4

2 回答 2

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))), '.', '')" />
于 2013-11-08T08:45:03.810 回答
0

您还可以使用translate()从字母数字值中删除前导零。例如,如果您有这样的 XML:

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

您可以像这样使用 XSLT:

<?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*是一个正则表达式,将删除所有前导零。如果你想结合点的翻译,你可以这样做:

<xsl:value-of select="translate(translate(., '^0*', ''), '.', '')" />
于 2013-11-08T08:49:28.900 回答