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: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 24 Mar 2005 11:15:43 -0000
I'm assuming the EintrdgeListe will actually hold values for times
throughout a single day, and you want to produce one Eintrag element for
each period of an hour within that day? Do you want to produce Eintrag
elements for periods that don't appear in the input? It would help to have a
clearer statement of the problem, it's hard to guess the general rules from
an example.

Here's an XSLT 2.0 solution which might do what you want:

<xsl:template match="EintrdgeListe">
  <xsl:for-each-group select="Eintrag" group-by="@BZ_Zeit mod 100">
    <Eintrag BZF_Zeit="{current-grouping-key()*100}"
             BZF_MengeGezdhlt="{sum(current-group()/@BZF_MengeGezdhlt}">
      <xsl:copy-of select="@* except (@BZ_Zeit, @BZF_MengeGezdhlt)"/>
    </Eintrag>
  </xsl:for-each-group>
</xsl:template>

Michael Kay
http://www.saxonica.com/



> -----Original Message-----
> From: michella@xxxxxxx [mailto:michella@xxxxxxx]
> Sent: 24 March 2005 10:54
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Grouping elements and summing their attribute
> value into one single element
>
> Hello,
>
> I'm currently having trouble choosing an efficient way to
> solve my problem :
>
> Here the XML :
>
> <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"/>
> ...
> </EintrdgeListe>
>
> And my problem : Here we have three elements. Their
> attributes are all similar, except BZF_MengeGezdhlt (the
> data) and BZF_Zeit , which is the time where this data was
> created. (0835 means 08.35 am.)
> Now, I have to compare these datas with other ones, but this
> comparison process is only done in segments of round hours
> (e.g. 08.00 o'clock is all summed element values between 0800
> and 0859).
> In the XML sample here, we have three elements that fit the
> selection criteria (they have been all created between 0800
> and 0859). I must create one single element (with same
> caracteristics), but the end value of BZF_MengeGezdhlt should
> be (58+47+42 = 147). Then the resulted element will be used
> for final 1-1 comparison.
>
> ==>  <Eintrag BZ_Abgangsdatum="20050211" BZ_Zeit="0800"
> BZ_DLA="B1" BZ_Format="NF" BZ_Verarbeitung="Maschine"
> BZ_Bundart="999" BZF_Ankunftsdatum="20050211" BZF_Zeit="1442"
> BZF_Zustelldatum="20050211" BZF_MengeGezdhlt="142"/>
>
> Any hint would be greatful.
>
> Best regards, and happy easter ;-)
>
> Lawrence Michel

Current Thread