Sorting by date in xslt
This is an open discussion with 6 replies, filed under XSLT.
Search
Fixed, using the following sort code (datum is the dutch word for date, which is the name of my date field):
<xsl:sort select="substring(datum/date/start/@iso,1,4)" data-type="number" order="descending" /> <xsl:sort select="substring(datum/date/start/@iso,6,2)" data-type="number" order="descending" /> <xsl:sort select="substring(datum/date/start/@iso,9,2)" data-type="number" order="descending" /> <xsl:sort select="substring(datum/date/start/@iso,12,2)" data-type="number" order="descending" /> <xsl:sort select="substring(datum/date/start/@iso,15,2)" data-type="number" order="descending" /> <xsl:sort select="substring(datum/date/start/@iso,18,2)" data-type="number" order="descending" />
What format is your date in? You might be able to achieve this in a single sort:
<xsl:sort select="translate(datum/date/start/@iso, '-', '')" data-type="number" order="descending" />
This is what I use on Symphony’s date fields, to remove the hyphens thereby converting to a sortable number.
The date is in iso format (including time), so simply stripping slashes won’t cut it, I’m afraid ;)
Do the following…
<xsl:sort select="translate(substring(datum/date/start/@iso,1,10), '-', '')" data-type="number" order="descending" />
Do the following…
I would really like to also sort on time.. So I think something like this:
<xsl:sort select="translate(substring(datum/date/start/@iso,1,10), '-', '')" data-type="number" order="descending" /> <xsl:sort select="translate(substring(datum/date/start/@iso,12,7),':','')" data-type="number" order="descending" />
would be the best way to go.
Thanks both!
Couldn’t you consolidate them into one with a concat()
?
<xsl:sort select="concat(translate(substring(datum/date/start/@iso,1,10), '-', ''),translate(substring(datum/date/start/@iso,12,7),':',''))" data-type="number" order="descending" />
Create an account or sign in to comment.
For a website I need to take multiple sections, and load the most recent entries from those sections.
As datasources only load from one section at a time, I can not use one datasource, and order the entries there (which is what I would prefer), but instead I have to create 4 datasources, let them load the most current 5 entries, and combine them in xslt.
This all works pretty well, except the ordering part. For some reason I can’t seem to figure out a reliable way to do so in xslt (I don’t have xslt 2 available).
What would be the best way of tackling this?