Search

I am implementing Muenchian Method to group entries by date and it is confusing. I get the list of fruits grouped by day in a list. Please see the sample data below… How can I count how many bananas and cherries that are eaten in each day.

<data>
    <dailyfruits>
        <year value="2010">
            <month value="08">
                <entry id="359">
                    <date time="14:30" weekday="7">2010-08-01</date>
                    <fruit>
                        <item handle="banana">Banana</item>
                    </fruit>
                </entry>
                <entry id="352">
                    <date time="14:00" weekday="7">2010-08-01</date>
                    <fruit>
                        <item handle="cherry">Cherry</item>
                    </fruit>
                </entry>
                 <entry id="352">
                    <date time="14:00" weekday="7">2010-08-01</date>
                    <fruit>
                        <item handle="banana">Banana</item>
                    </fruit>
                </entry>
            </month>
        </year>
    <dialifruits>
<data>

This is the xsl:

<xsl:key name="fruits-by-day" match="entry" use="date"/>
    <xsl:template match="/">
        <xsl:apply-templates select="data/dailyfruits/year/month/entry[count(.|key('fruits-by-day', date)[1]) = 1]" mode="gfc"/>
    </xsl:template>
    <xsl:template match="entry" mode="gfc">
        <xsl:param name="fruit-date" select="date"/>
        <h2><xsl:value-of select="$fruit-date"/></h2>
        <ul><xsl:apply-templates select="key('fruits-by-day',date)" mode="gfc-result"/></ul>        
        </xsl:template>
    <xsl:template match="entry" mode="gfc-result">
        <li><xsl:value-of select="fruit/item"/></li>
    </xsl:template>

I think I figured it out on my own. Can somebody please comment if it’s the right way.

Cherries: <xsl:value-of select="count(key('fruits-by-day',date)/fruit/item[@handle='cherry'])" />   
Bananas: <xsl:value-of select="count(key('fruits-by-day',date)/fruit/item[@handle='banana'])" />
Total: <xsl:value-of select="count(key('fruits-by-day', date))" />

If I test it, the numbers add up…

Looks good to me!

I learned that it’s always good to use keys if you are doing some kind of search on your data. (One reason is performance, by the way.)

That will work if you know all the types of fruit that will be entered. When an unexpected type is entered your total won’t match the sum of the enumerated fruit. And you will have some zero counts on dates that don’t include a particular type of fruit.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details