7 users online. Create an account or sign in to join them.Users
Muenchian Method - count items in group
This is an open discussion with 3 replies, filed under General.
Search
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.
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>