使用 xsl1.0 或 xslt 2.0 删除尾随零

Remove Trailing zeros using xsl1.0 or xslt 2.0

我必须对 xml 中的值求和并从 xml 中删除尾随零。

你能帮我用 xsl1.0 或 xslt2.0 删除这个吗

我试过使用 number(.) 但它没有删除尾随的零。

我的输入低于

<test>
    <loop>
        <lines>
            <linesTotal>2010</linesTotal>
        </lines>
        <lines>
            <linesTotal>20</linesTotal>
        </lines>
    </loop>
</test>

预期输出是 203

但结果是 2030

请帮帮我!

您可以使用 2.0 的替换功能:

replace(string(2010+30), '0$', '')

我不明白为什么这会有用(至少在给定的示例中没有用),我强烈怀疑您真的不想这样做。

但是 - 主要是为了好玩 - 这是一种从结果中删除任何尾随零的方法:

XSLT 1.0

<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="/test">
    <output>
        <xsl:call-template name="remove-trailing-zeros">
            <xsl:with-param name="number" select="sum(loop/lines/linesTotal)"/>
        </xsl:call-template>
    </output>
</xsl:template>

<xsl:template name="remove-trailing-zeros">
    <xsl:param name="number"/>
    <xsl:choose>
        <xsl:when test="$number and not($number mod 10)">
            <xsl:call-template name="remove-trailing-zeros">
                <xsl:with-param name="number" select="$number div 10"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$number"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

在 XSLT 2.0 中,您可以将其设为函数而不是模板。或者将问题移动到字符串域(如另一个答案中所建议的):

<xsl:template match="/test">
    <xsl:param name="sum" select="sum(loop/lines/linesTotal)"/>
    <output>
        <xsl:value-of select="replace(string($sum), '0+$', '')"/>
    </output>
</xsl:template>