RE: [xsl] sum (substring-before)

Subject: RE: [xsl] sum (substring-before)
From: "Sullivan, Dan" <dsullivan@xxxxxxxxxxx>
Date: Fri, 24 Aug 2001 10:55:01 -0700
Given an input file like:

<values>
	<value>10.00CR</value>
	<value>5.00CR</value>
	<value>5.00CR</value>
	<value>20.00</value>
	<value>20.00</value>
</values>


This will do the totalization (watch for line wraps):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                version="1.0">
  <xsl:template name="negative">
    <xsl:param name="value"/>
    <!-- get the following credit, if there is one -->
    <xsl:variable name="following"
select="$value/following-sibling::value[contains(.,'CR')][1]"/>
    <xsl:variable name="next">
      <xsl:if test="$following">
        <!-- there is a following credit, means there might be more -->
        <xsl:call-template name="negative">
          <xsl:with-param name="value" select="$following"/>
        </xsl:call-template>
      </xsl:if>
      <!-- there is no following credit no make the next on a 0 -->
      <xsl:if test="not($following)">0</xsl:if>
    </xsl:variable>
    <!-- build up the total of credits -->
    <xsl:value-of select="number(substring-before($value, 'CR')) +
number($next)"/> 
  </xsl:template>

  <xsl:template match="/values">
    <!-- find first credit -->
    <xsl:variable name="credit"
select="/values/value[contains(.,'CR')][1]"/>
    <total>
      <!-- if there is a first credit there might be more -->
      <xsl:variable name="negTotal">
        <xsl:if test="$credit">
          <!-- use negative template to sum up all credits -->
          <xsl:call-template name="negative">
            <xsl:with-param name="value"  select="$credit"/>
          </xsl:call-template>
        </xsl:if>
        <!-- if there isn't another credit them make this one 0 -->
        <xsl:if test="not($credit)">0</xsl:if>
      </xsl:variable>
      <!-- add up all the debits and subtract the credits -->
      <xsl:value-of select="sum($debits) - $negTotal"/>
    </total>
  </xsl:template>
  <!-- make a nodelist of all of the debits -->
  <xsl:variable name="debits" select="/values/value[not(contains(.,
'CR'))]"/>
</xsl:stylesheet>


Dan

-----Original Message-----
From: Winnie Leung [mailto:wleung@xxxxxxxxxxxxxx]
Sent: Friday, August 24, 2001 10:31 AM
To: XSL-List@xxxxxxxxxxxxxxxxxxxxxx
Subject: [xsl] sum (substring-before)


dear all,

is it possible to do a sum and a substring-before together?
my data:
20.00
10.00CR
5.00CR

(CR means negative)

and i want to get a total of all (i.e. 20.00+(-10.00)+(-5.00) = 5)

thanks in advance,
wing


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread