This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Sum Problem
- From: "Kirwan, K." <K dot Kirwan at CiTG dot TUDelft dot NL>
- To: "'xsl-list at lists dot mulberrytech dot com'" <xsl-list at lists dot mulberrytech dot com>
- Date: Wed, 29 May 2002 19:51:03 +0200
- Subject: [xsl] Sum Problem
- Reply-to: xsl-list at lists dot mulberrytech dot com
Dear All,
I've an XML file as follows-
<section id="A">
<item id="1" value="1"/>
<item id="2" value="2"/>
<item id="3" value="2"/>
<item id="4" value="2"/>
</section>
<section id="B">
<item id="1" value="1"/>
<item id="2" value="10"/>
<item id="3" value="3"/>
<item id="4" value="4"/>
</section>
I would like to sum CERTAIN values within the above xml as follows:
1- starting conditions $index=1 $total=0
2- select sectionA/item[@index=$index] (has @value=1)
3- calculate $total=$total+@value (=0+1)
4- calculate $index=$index+@value (=1+1)
5- select sectionB/item[@index=$index] (item2 which has
value=10)
6- repeat steps 3-4 using new $index -> $total=1+10,
index=2+10
I'm guessing this requires some form of recursion but can't figure out how
to pass
extracted parameters back to the calling template.
i.e. i'd like to do something like this with the updated totals returned to
the calling root-template.
----------------------------------------------------------------------------
----------------------------------------------------------------------------
----------------------------------------------------------------------------
---------------------
<?xml version="1.0"?>
<xsl:transform
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml"/>
<xsl:param name="A" select="'A'"/>
<xsl:param name="B" select="'B'"/>
<xsl:param name="index" select="1"/>
<xsl:param name="total" select="0"/>
<xsl:template match="/">
<sections>
<section1>
<xsl:apply-templates select="sections/section[@id=$A]/item[@id=$index]">
<xsl:with-param name="index" select="$index"/>
<xsl:with-param name="total" select="$total"/>
</xsl:apply-templates>
</section1>
<section2>
<xsl:apply-templates select="sections/section[@id=$A]/item[@id=$index]">
<xsl:with-param name="index" select="$index"/>
<xsl:with-param name="total" select="$total"/>
</xsl:apply-templates>
</section2>
</sections>
</xsl:template>
<xsl:template match="item">
<xsl:param name="index">
<xsl:param name="total">
<xsl:param name="total"><xsl:value-of select="$total+@value"/></xsl:param>
<xsl:value-of select="$total+@value"/>
</xsl:template>
</xsl:transform>
Kieran Kirwan
Researcher,
Transportation and Traffic Engineering Section,
Civiele Techniek, TU Delft,
Stevinweg 1, 2600GA Delft, Netherlands
phone: +31152785061
mobile: +31641690151
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list