At 2004-01-28 19:22 -0800, Charles Gebhard wrote:
i'm a newbie and am trying to get a sum of a calculation; i'm hoping to
get the value "162.00".
...
so, i'd really like to be able to do this in native XSLT 1.0 (without
node-set). could someone please point me in the right direction?
Using a recursive-like "walk" of the source node tree, performing
calculations at each step of the walk.
seems like it would be really simple.
If you don't mind recursive-like coding.
i've been pounding my head for hours.
I hope the working example demonstrated below provides relief. Note how I
start at the first RECEIPT element, initializing the result to zero, then
at each RECEIPT I perform the local calculation and update a running
result. Then, if there is a sibling, pass this intermediate result to the
sibling processing of the closest following sibling, using a mode to ensure
other templates in my stylesheet don't interfere. If there is no sibling,
return the value calculated so far.
The template calling returns a string, so the variable is initially a text
string in a result tree fragment node, that I then cast to a number for
other checking and calculations (this has to be done for checking because
all result tree fragments test as true).
Note the trick in the variable declaration where I have a leading zero
followed by the string calculated by the templates: this way, if there are
no RECEIPT elements at all, the string at least contains a zero digit to be
converted to a zero result ... otherwise you would get a NaN result.
................. Ken
p.s. I note your company is based in San Francisco ... we are scheduled to
teach XSL in a publicly subscribed course in Burlingame next month at the
SFO Embassy Suites hotel; details linked from our home page.
T:\ftemp>type gebhard.xml
<BATCH>
<RECEIPT>
<qtyRcpt>1</qtyRcpt>
<line>
<pr>102.00</pr>
</line>
</RECEIPT>
<RECEIPT>
<qtyRcpt>3</qtyRcpt>
<line>
<pr>20.00</pr>
</line>
</RECEIPT>
</BATCH>
T:\ftemp>type gebhard.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:variable name="result-text">
<xsl:text/>0<xsl:apply-templates select="/*/RECEIPT[1]" mode="equation"/>
</xsl:variable>
<xsl:variable name="result" select="number($result-text)"/>
<xsl:value-of select="$result"/>
</xsl:template>
<xsl:template match="RECEIPT" mode="equation">
<xsl:param name="result" select="0"/>
<xsl:variable name="this" select="$result + ( qtyRcpt * line/pr )"/>
<xsl:choose>
<xsl:when test="following-sibling::RECEIPT">
<xsl:apply-templates select="following-sibling::RECEIPT[1]"
mode="equation">
<xsl:with-param name="result" select="$this"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$this"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
T:\ftemp>saxon gebhard.xml gebhard.xsl
<?xml version="1.0" encoding="utf-8"?>162
T:\ftemp>
--
Public courses: sign up for one or both soon to reserve your seat!
Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO
Washington, DC: 2004-03-15 San Francisco, CA: 2004-03-22
Hong Kong, China: 2004-05-17 Bremen, Germany: 2004-05-24
World-wide on-site corporate, government & user group XML training
G. Ken Holman mailto:gkholman(_at_)CraneSoftwrights(_dot_)com
Crane Softwrights Ltd. http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0 +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness http://www.CraneSoftwrights.com/s/bc
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list