xsl-list
[Top] [All Lists]

RE: Grouping elements and summing their attribute value into one single element

2005-03-24 04:15:43
I'm assuming the EinträgeListe 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="EinträgeListe">
  <xsl:for-each-group select="Eintrag" group-by="@BZ_Zeit mod 100">
    <Eintrag BZF_Zeit="{current-grouping-key()*100}"
             BZF_MengeGezählt="{sum(current-group()/@BZF_MengeGezählt}">
      <xsl:copy-of select="@* except (@BZ_Zeit, @BZF_MengeGezählt)"/>
    </Eintrag>
  </xsl:for-each-group>
</xsl:template>

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



-----Original Message-----
From: michella(_at_)post(_dot_)ch [mailto:michella(_at_)post(_dot_)ch] 
Sent: 24 March 2005 10:54
To: xsl-list(_at_)lists(_dot_)mulberrytech(_dot_)com
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 :

<EinträgeListe>
      <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_MengeGezählt="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_MengeGezählt="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_MengeGezählt="42"/>
...
</EinträgeListe>

And my problem : Here we have three elements. Their 
attributes are all similar, except BZF_MengeGezählt (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_MengeGezählt 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_MengeGezählt="142"/>

Any hint would be greatful.

Best regards, and happy easter ;-)

Lawrence Michel

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: 
<mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--





--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--



<Prev in Thread] Current Thread [Next in Thread>