0

I have this XML:

<events>
  <event name="Christmas" attendees="1"/>
  <event name="Halloween" attendees="3"/>
  <event name="Easter" attendees="2"/>
  <event name="Easter" attendees="1"/>
</events>

Thanks to hr_117's help, I managed to do this:

<xsl:template match="data">
  <xsl:apply-templates select="events"/>
</xsl:template>

<xsl:key name="events-by-name" match="events/event" use="@name" />

<xsl:template match="events">
<xsl:for-each select="event[count(. | key('events-by-name', @name)[1]) = 1]">
    <p>
        <xsl:value-of select="concat(@name,': ')" />
        <xsl:value-of select="count(key('events-by-name', @name))" />
        <xsl:text> booking(s)</xsl:text>        
    </p>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Which gives me this output:

Christmas: 1 booking(s)
Halloween: 1 booking(s)
Easter: 2 booking(s)

But how can I count the attendees as well, so that I get this:

Christmas: 1 booking(s), 1 attendee(s)
Halloween: 1 booking(s), 3 attendee(s)
Easter: 2 booking(s), 1 attendee(s)

Can anybody help?

Thanks!

Community
  • 1
  • 1
Tintin81
  • 9,821
  • 20
  • 85
  • 178

1 Answers1

3

How about this? I tried it with the sum method and I can see the accumulated value, I also added some additional elements for testing.

<xsl:value-of select="concat(@name,': ')"/>
<xsl:value-of select="count(key('events-by-name', @name))"/>
<xsl:text> booking(s), </xsl:text>
<xsl:value-of select="sum(key('events-by-name', @name)/@attendees)"/>
<xsl:text> attendee(s)</xsl:text>
Niraj
  • 66
  • 4
  • That works! Thanks a lot for your help. However, I got stuck one more time because my actual XML is slightly different to the one I posted above for reasons of simplicity. I decided to open [another thread for this](http://stackoverflow.com/questions/16584221/how-to-improve-my-muenchian-grouping-xslt). This XSLT stuff is really causing me a headache. So glad you guys can help me out on this. – Tintin81 May 16 '13 at 09:57