Sort by date in XSL
I am trying to sort by date for XML output. Here is my XSL:
<?xml version="1.0" encoding="UTF-8"?>
<!-- Example by Phil 'iwonder' Guerra -->
<!-- Edited by Lee Sykes DNN Creative Magazine http://www.dnncreative.com -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes"/>
<xsl:param name="TITLE"/>
<xsl:template match="rss">
<!-- Do not show channel image -->
<!-- Do not select the first item (this is useful if there are advertisements, such as using an RSS feed from moreover.com-->
<xsl:for-each select="channel/item[position() > 0]">
<!-- Test to limit number of items displayed. Here only 5 items will be transformed -->
<xsl:if test="position() < 5">
<br></br>
<!-- to open links in a new window, change target="_main" to target="_blank" -->
<strong><a href="{link}" target="_blank"><xsl:value-of select="title"/></a></strong>
<br>
<!-- <xsl:value-of select="pubDate"/> -->
</br>
<!-- only display 100 characters of the description, and allow html -->
<xsl:value-of disable-output-escaping="yes" select="description"/>
</xsl:if>
</xsl:for-each>
</xsl:template>
<xsl:template match="description">
<br>
<xsl:value-of select="."/>
</br>
</xsl:template>
<xsl:template name="strip-tags">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '<')">
<xsl:value-of select="substring-before($text, '<')" />
<xsl:call-template name="strip-tags">
<xsl:with-param name="text" select="substring-after($text, '>')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="substring($text, 1, 100)" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I am trying to sort descending using the entereddate in my XML:
<item>
<title>Media Director</title>
<entereddate>4/2/2009</entereddate>
<referencenumber>01646359</referencenumber>
<description><![CDATA[Leading Cleveland-based Award-Winning Integrated Marketing Communications firm seeks a passionate Media Director to lead a team of media planning and buying professionals and Fortune 500 clients. This individual will be responsible to develop strategic and innovative traditional and nontraditional consumer engagement solutions including emerging media. This leader will play a large role in new business. Ten years experience in media planning and buying as well as five years of management required. The environment is collaborative with many creative perks.]]></d开发者_开发问答escription>
<city>Cleveland</city>
<state>OH</state>
<country>United States of America</country>
<salary>$0.00 - $0.00 / $0.00/hr - $0.00/hr</salary>
<guid isPermaLink="false">http://employment.topechelon.com/web77391/jobseeker/sSetup.asp?runsearch=1&spJobAdId=01646359</guid>
<link>http://employment.topechelon.com/web77391/jobseeker/sSetup.asp?runsearch=1&spJobAdId=01646359</link>
</item>
Any help would be appreciated!
ThanksDates in XML documents should always be formatted as YYYY-MM-DD. This is the format that the XML Schema date
data type uses, and it's a format that can be both readily sorted by in XSLT and manipulated using XSLT's limited string functions.
If the dates are always in a regular format (eg mm/dd/yyyy), you can use 3 sort keys.
<xsl:for-each select="channel/item[position() > 0]">
<xsl:sort select="substring-after(substring-after(entereddate,'/'),'/')" data-type="number" /> <!-- year -->
<xsl:sort select="substring-before(entereddate,'/')" data-type="number" /> <!-- month -->
<xsl:sort select="substring-before(substring-after(entereddate,'/'),'/')" data-type="number" /> <!-- day -->
</xsl:for-each>
However if the dates may be in other formats such as "Mar 13, 2010", you will need to parse and convert the dates into a sortable format (yyyymmdd).
Exslt has includes extensions functions for manipulating dates. The comments in your stylesheet suggest that you are using .NET, exslt is available for .NET from the MvpXml project.
XSLT can help with sorting, but isn´t any good with dates. AFAIK, the easiest way to solve this is generate a string with `CCYYMMDD´ (Century, Year, Month, Day) from the date field you have and use that to sort alphabetically when selecting. Use the text ´as is´ when outputting.
combination of <xsl:sort ...> and substring(datefield,xx,2)+substring...
Hope this helps,
精彩评论