[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: sum (substring-before)

Subject: RE: sum (substring-before)
From: "Sullivan, Dan" <dsullivan@xxxxxxxxxxx>
Date: Fri, 24 Aug 2001 10:55:01 -0700
substring before
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:  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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.