This is the mail archive of the
xsl-list@mulberrytech.com
mailing list .
Re: combine node values
- From: "J.Pietschmann" <j3322ptm at yahoo dot de>
- To: xsl-list at lists dot mulberrytech dot com
- Date: Thu, 19 Sep 2002 21:18:00 +0200
- Subject: Re: [xsl] combine node values
- References: <3D8A07E2.5253D612@Sun.COM>
- Reply-to: xsl-list at lists dot mulberrytech dot com
edwin colaco wrote:
The result I get with this XSL template is
Col1 Col2 Col3
aaa 2 1
bbb 2 1
aaa 3 0
bbb 2 1
I would like to get the following result instead
Col1 Col2 Col3
aaa 5 1
bbb 4 2
It seems you hit a standard problem called "grouping", in your
case by the value of EEE/@name. You can inform yourself about
background and solutions in the XSL FAQ
http://www.dpawson.co.uk/
or at Jeni Tennisons site
http://www.jenitennison.com/xslt/grouping/index.html
The standard technique is usually called Muenchean Grouping
(after the inventor).
Define a key
<xsl:key name="eee-name-group" match="EEE" use="@name"/>
This key defines the groups.
In the for-each, check whether the element iss the first of
the group as selected by the key. Use the key again to get all
EEE elements grouped by their name to accumulate the values you
need:
<xsl:for-each select="/AAA/BBB/CCC/DDD/EEE[generate-id()=generate-id(key('eee-name-group',@name)[1])]">
<xsl:variable name="nodes" select="key('eee-name-group',@name)"/>
<xsl:variable name="ct" select="count($nodes/FFF/GGG)"/>
<xsl:variable name="okct" select="count($nodes/FFF/GGG[status='ok'])"/>
<xsl:variable name="failct" select="count($nodes/FFF/GGG[status='fail'])"/>
<xsl:variable name="successRate" select="((($ct)-($failct)) div ($ct))"/>
...
HTH
J.Pietschmann
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list