Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
603 views
in Technique[技术] by (71.8m points)

xslt - XSL - Sum of computed amounts

I just need some help on basic feature of XSL. I would like to display a sum of amounts previously computed. But I do not know how to do it. For information the XSL must work with XSLT 1.0, technical limitation on my side.

For instance here is my xml.

<A>
   <amount>10</amount>
   <rate>4</rate>
</A>
<A>
   <amount>-21</amount>
   <rate>2</rate>
</A>
<B>
   <amount>8</amount>
   <rate>1</rate>
</B>
<C>
   <amount>7</amount>
   <rate>32</rate>
</C>

and I would like to display the sum of each amount multiplied by each associated rate within a Total element.

<Total value="230">
    <PositiveTotal>
        272
    </PositiveTotal>
    <NegativeTotal>
        -42
    </NegativeTotal>
</Total>

I have no idea how to do it.

Thanks in advance

Regards,

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

One of possible multiple solutions. It will give you an idea, how to solve this.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="root">
        <xsl:variable name="positiveTotal">
            <xsl:call-template name="sum">
                <xsl:with-param name="items" select="*[not(starts-with(amount,'-') or starts-with(rate, '-'))]"/>
            </xsl:call-template>
        </xsl:variable>

        <xsl:variable name="negativTotal">
            <xsl:call-template name="sum">
                <xsl:with-param name="items" select="*[starts-with(amount,'-') or starts-with(rate, '-')]"/>                
            </xsl:call-template>
        </xsl:variable>

        <Total value="{$positiveTotal + $negativTotal}">
            <PositivTotal>
                <xsl:value-of select="format-number($positiveTotal, '0')"/>
            </PositivTotal>
            <NegativeTotal>
                <xsl:value-of select="format-number($negativTotal, '0')"/>
            </NegativeTotal>
        </Total>

    </xsl:template>

    <xsl:template name="sum">
        <xsl:param name="items" />
        <xsl:param name="total" select="0" />

        <xsl:choose>
            <xsl:when test="not($items)">
                <xsl:value-of select="$total"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:call-template name="sum">
                    <xsl:with-param name="items" select="$items[position() > 1]" />
                    <xsl:with-param name="total"
                        select="$total + ($items[1]/amount * $items[1]/rate)" />
                </xsl:call-template>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:template>

</xsl:stylesheet>

!! Change match="root" to your root-node! Given source-xml is not valid.

There are already many sum-questions! See the Related Box on your right side of screen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...