10 users online. Create an account or sign in to join them.Users
Group by Date
This is an open discussion with 17 replies, filed under Troubleshooting.
Search
Reading another post that this could be done by using the Key Element.
Need to dig further.
Hmm, can this even be done? I need to display fixtures grouped by date (Day, Month, Year).
You could group your data source by a date field which will sort your XML by year and month. You could than sort your months by day using <xsl:sort /> in your XSLT.
I have grouped by Date.
This is my XSLT:
<xsl:for-each select="superleague-mens-fixtures/year/month">
<h4 class="date">
<xsl:call-template name="format-date">
<xsl:with-param name="date" select="entry/date"/>
<xsl:with-param name="format" select="'D m Y'"/>
</xsl:call-template>
</h4>
<ul>
<xsl:for-each select="./entry">
<li><xsl:value-of select="home-team/item"/> v <xsl:value-of select="away-team"/></li>
</xsl:for-each>
</ul>
</xsl:for-each>
Here is some XML:
<superleague-mens-fixtures>
<section id="4" handle="superleague-mens-fixtures-and-results">Superleague Men's Fixtures and Results</section>
<year value="2009">
<month value="08">
<entry id="85">
<date time="12:00" weekday="7">2009-08-23</date>
<home-team>
<item handle="tradesmans-arms-b">Tradesmans Arms B</item>
</home-team>
<away-team>
<item handle="bye">Bye</item>
</away-team>
<average>None</average>
</entry>
<entry id="77">
<date time="11:46" weekday="7">2009-08-09</date>
<home-team>
<item handle="corporation-arms">Corporation Arms</item>
</home-team>
<away-team>
<item handle="stanton-house">Stanton House</item>
</away-team>
<average>None</average>
</entry>
</month>
</year>
</superleague-mens-fixtures>
That looks interesting, So I will be able to have this kind of effect:
Date
Fixtures on that date
Date
Fixtures on that date
etc…
Yes indeed:
Friday, 28 August 2009
- Fixture 1
- Fixture 2
Saturday, 29 August 2009
- Fixture 3
- Fixture 4
I use this technique to group articles for blog archives.
Ok, well this is a technique that I am new at, I may be posting some SOS type stuff.
This is what I have so far.
<xsl:key name="fixture-dates" match="entry" use="date"/>
<xsl:template name="sl-mens-fixtures">
<xsl:for-each select="superleague-mens-fixtures/year/month[key('fixture-dates',date)]">
</xsl:for-each>
</xsl:template>
Hey Nick try this…
Based on your example XML posted above, I added another team (Team B) to your XML with the same date, so we could test the grouping.
XML
<?xml version="1.0" encoding="UTF-8"?>
<data>
<superleague-mens-fixtures>
<section id="4" handle="superleague-mens-fixtures-and-results">Superleague Men's Fixtures and Results</section>
<year value="2009">
<month value="08">
<entry id="85">
<date time="12:00" weekday="7">2009-08-23</date>
<home-team>
<item handle="tradesmans-arms-b">Tradesmans Arms B</item>
</home-team>
<away-team>
<item handle="bye">Bye</item>
</away-team>
<average>None</average>
</entry>
<entry id="86">
<date time="11:00" weekday="7">2009-08-23</date>
<home-team>
<item handle="test-b">Test B</item>
</home-team>
<away-team>
<item handle="bye">Bye</item>
</away-team>
<average>None</average>
</entry>
<entry id="77">
<date time="11:46" weekday="7">2009-08-09</date>
<home-team>
<item handle="corporation-arms">Corporation Arms</item>
</home-team>
<away-team>
<item handle="stanton-house">Stanton House</item>
</away-team>
<average>None</average>
</entry>
</month>
</year>
</superleague-mens-fixtures>
</data>
Here’s the XSL (via Muenchian Method)…
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes" omit-xml-declaration="yes"/>
<xsl:key name="fixture-dates" match="entry" use="date"/>
<xsl:template match="/">
<xsl:apply-templates select="data/superleague-mens-fixtures/year/month/entry[count(.|key('fixture-dates', date)[1]) = 1]" mode="fixture-group"/>
</xsl:template>
<xsl:template match="entry" mode="fixture-group">
<xsl:param name="fixture-date" select="date"/>
<h2><xsl:value-of select="$fixture-date"/></h2>
<ul>
<xsl:apply-templates select="key('fixture-dates',date)" mode="fixture-group-teams"/>
</ul>
</xsl:template>
<xsl:template match="entry" mode="fixture-group-teams">
<li><xsl:value-of select="home-team/item"/></li>
</xsl:template>
</xsl:stylesheet>
Try that and see if works.
Nick, if you don’t mind. Let me know if that works.
I had to make some slight amends to the XPath, but it works.
Can I ask though, so I can learn - what does this mean?
<xsl:apply-templates select="superleague-mens-fixtures/year/month/entry[count(.|key('fixture-dates', date)[1]) = 1]" mode="fixture-group"/>
Jeni Tennison explains it well on her site (better than I can). That’s where I got the technique.
Ok thanks, I’ll revise that.
No problem. I hope that was a help.
Read it and I don’t understand why they have to choose from the first record.
You are grouping by date, therefore you want to assign a key value to the date. So when the key value or date repeats you only need to use show the date node value once.
So, for instance in the example XML above, there are two events on 2009-08-23. So for it to read…
August 23, 2009
- Tradesmans Arms B (Bye)
- Test B (Bye)
We have to assign that key value on the date. We don’t want to show that date twice, that’s why we match the first entry where that date shows up. Does that help?
@Allen or any of the other XSL ninjas, could y’all help me with this explanation on Muenchian Method of grouping?
Ok, so then it displays all data that matches that node and then moves onto the next node?
Yep. That’s my understanding. It displays all the data that have similar keys, so in this case all of the ones with the same date, and then it moves on finds the next keyed date and all the information that goes along with that.
Create an account or sign in to comment.
Is it possible for me to group all entries that have the same day/month/year entry? and have that date shown prior to the events?
For example here: Fixtures
All of those games are played on the same date, so I would like that data to be preceded with a Date. Can it be done?