有没有办法使用 XSLT 从字符串中获取数字并计算相同的总和? [英] Is there any way to get number out of String and Calculate Sum of the same using XSLT?

查看:22
本文介绍了有没有办法使用 XSLT 从字符串中获取数字并计算相同的总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

字符串例如-

         10 AL @ 6' X 32' ROOFTOP
          5 AL @ 6' X 32' ROOFTOP
          4 AL @ 6' X 32' ROOFTOP
          6 AL @ 6' X 32' ROOFTOP

我需要提取 AL 之前的所有数字并从中计算总和.

I need to get extract all the number before AL and calculate sum out of it.

我试过 <sum(substring-before(stringName,' AL')/> 但我得到了 NaN 作为输出.

I tried with < sum(substring-before(stringName,' AL') /> but I got NaN as output.

来自评论:

<part_d>
    <description label="Description Part">1 RL @ 4' X 32'</description>
    <description label="Description Part">10 RL @ 4' X 32'</description> 
    <description label="Description Part">5 RL @ 4' X 32'</description> 
    <description label="Description Part">8 RL @ 4' X 32'</description> 
    <description label="Description Part">9 RL @ 4' X 32'</description> 
</part_d>

推荐答案

XSLT 1.0 中的 sum() 函数只能求和,不能计算.试试这个方法:

The sum() function in XSLT 1.0 can only sum numbers, not calculations. Try it this way:

XSLT 1.0 (+EXSLT)

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:template match="part_d">
    <xsl:variable name="quantities">
        <xsl:for-each select="description">
            <q>
                <xsl:value-of select="substring-before(., ' ')" />
            </q>
        </xsl:for-each>
    </xsl:variable>
    <total-quantity>
        <xsl:value-of select="sum(exsl:node-set($quantities)/q)" />     
    </total-quantity>
</xsl:template>

</xsl:stylesheet>

或者,如果您愿意:

XSLT

<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="part_d">
    <total-quantity>
        <xsl:call-template name="add">
            <xsl:with-param name="items" select="description"/>
        </xsl:call-template>
    </total-quantity>
</xsl:template>

<xsl:template name="add">
    <xsl:param name="items" select="/.."/>
    <xsl:param name="sum" select="0"/>
    <xsl:choose>
        <xsl:when test="$items">
            <xsl:variable name="item" select="$items[1]" />
            <xsl:variable name="n" select="substring-before($item, ' ')" />
            <!-- recursive call -->
            <xsl:call-template name="add">
                <xsl:with-param name="items" select="$items[position() > 1]"/>
                <xsl:with-param name="sum" select="$sum + $n"/>
            </xsl:call-template>
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$sum"/>
        </xsl:otherwise>
    </xsl:choose>
</xsl:template>

</xsl:stylesheet>

这篇关于有没有办法使用 XSLT 从字符串中获取数字并计算相同的总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆