Re: [xsl] Grouping elements and summing their attribute value into one single element

Subject: Re: [xsl] Grouping elements and summing their attribute value into one single element
From: David Carlisle <davidc@xxxxxxxxx>
Date: Thu, 24 Mar 2005 11:14:27 GMT
Just use the usua; grouping techniques (eg keys as below) to group, and
then use sum() to sum all elements of a group.

eg


<?xml version="1.0" encoding="iso-8859-1"?>
<EintrdgeListe>
	<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0811" BZ_DLA="B1" BZ_Format="NF" BZ_Verarbeitung="Maschine" BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit="1442" BZF_Zustelldatum="20050211" BZF_MengeGezdhlt="58"/>
	<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0835" BZ_DLA="B1" BZ_Format="NF" BZ_Verarbeitung="Maschine" BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit="1442" BZF_Zustelldatum="20050211" BZF_MengeGezdhlt="47"/>
	<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0847" BZ_DLA="B1" BZ_Format="NF" BZ_Verarbeitung="Maschine" BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit="1442" BZF_Zustelldatum="20050211" BZF_MengeGezdhlt="42"/>
...
	<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0947" BZ_DLA="B1" BZ_Format="NF" BZ_Verarbeitung="Maschine" BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit="1442" BZF_Zustelldatum="20050211" BZF_MengeGezdhlt="42"/>
</EintrdgeListe>






<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
                xmlns:om="http://www.openmath.org/OpenMath";
                version="1.0">

<xsl:output indent="yes"/>

<xsl:key name="e" match="Eintrag" use="substring(@BZ_Zeit,1,2)"/>

<xsl:template match="EintrdgeListe">
<xsl:for-each select="Eintrag[generate-id()=generate-id(key('e',substring(@BZ_Zeit,1,2)))]">
<xsl:copy>
<xsl:copy-of select="@*"/>
<xsl:attribute name="BZ_Zeit"><xsl:value-of select="substring(@BZ_Zeit,1,2)"/>00</xsl:attribute>
<xsl:attribute name="BZF_MengeGezdhlt"><xsl:value-of select="sum(key('e',substring(@BZ_Zeit,1,2))/@BZF_MengeGezdhlt)"/></xsl:attribute>
</xsl:copy>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>




$ saxon  sum.xml sum.xsl
<?xml version="1.0" encoding="utf-8"?>
<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0800" BZ_DLA="B1" BZ_Format="NF" B
_Verarbeitung="Maschine" BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit
"1442" BZF_Zustelldatum="20050211" BZF_MengeGez@qhlt="147"/>
<Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0900" BZ_DLA="B1" BZ_Format="NF" B
_Verarbeitung="Maschine" BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit
"1442" BZF_Zustelldatum="20050211" BZF_MengeGez@qhlt="42"/>

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________

Current Thread