我正在尝试按日期对XML输出进行排序。下面是我的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>我正在尝试使用XML中的entereddate进行降序排序:
<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.]]></description>
<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>任何帮助都将不胜感激!
谢谢
发布于 2010-03-13 08:52:40
XML文档中的日期格式应始终为YYYY-MM-DD。这是XML Schema date数据类型使用的格式,这种格式既可以在date中方便地进行排序,也可以使用XSLT中有限的字符串函数进行操作。
发布于 2010-03-13 11:29:00
如果日期始终采用常规格式(例如mm/dd/yyyy),则可以使用3个排序关键字。
<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>但是,如果日期可能是其他格式,如"Mar 13,2010",则需要解析日期并将其转换为可排序的格式(yyyymmdd)。
Exslt包含用于操作日期的扩展函数。样式表中的注释表明您正在使用.NET,MvpXml项目中的.NET可以使用exslt。
发布于 2010-03-13 02:58:56
XSLT可以帮助排序,但不适合日期。AFAIK,解决这个问题的最简单方法是从日期字段中生成一个带有‘`CCYYMMDD’(世纪、年、月、日)的字符串,并在选择时使用该字符串按字母顺序排序。输出时使用文本“原样”。
,xx,2)+子字符串的组合...
希望这能帮上忙
https://stackoverflow.com/questions/2434403
复制相似问题