如何在 xslt 1.0 中将数字相互相乘

How to multiply numbers with each other in xslt 1.0

我有一个数字,例如 123,它存储在一个元素中,比方说 <Number>123</Number>。我正在寻找一个用 XSLT 1.0 编写的解决方案,它可以执行以下操作:1*2*3 并为我提供结果 6。Number 元素中的值可以是任意长度。我知道我可以通过子字符串函数并通过将值一个一个地存储在变量中来做到这一点,但问题是,我不知道这个字段的长度。

我无法为此编写任何 xslt。

任何人都可以为此提供帮助或建议解决方案吗?

您可以通过调用递归命名模板来完成此操作:

<xsl:template name="digit-product">
    <xsl:param name="digits"/>
    <xsl:param name="prev-product" select="1"/>
    <xsl:variable name="product" select="$prev-product * substring($digits, 1, 1)" />
    <xsl:choose>
        <xsl:when test="string-length($digits) > 1">
            <!-- recursive call -->
            <xsl:call-template name="digit-product">
                <xsl:with-param name="digits" select="substring($digits, 2)"/>
                <xsl:with-param name="prev-product" select="$product"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$product"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

工作示例:http://xsltransform.net/3NJ38YJ

请注意,这假定传递的 digits 参数的值为整数。