我正在尝试按日期排序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中的输入日期对降序进行排序:
<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>
任何帮助将不胜感激! 感谢
答案 0 :(得分:1)
XML文档中的日期应始终格式化为YYYY-MM-DD。这是XML Schema date
数据类型使用的格式,它是一种可以在XSLT中轻松排序并使用XSLT的有限字符串函数进行操作的格式。
答案 1 :(得分:1)
如果日期始终采用常规格式(例如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>
但是,如果日期可能采用其他格式,例如“2010年3月13日”,则需要解析并将日期转换为可排序格式(yyyymmdd)。
Exslt包含用于操作日期的扩展函数。样式表中的注释表明您使用的是.NET,exslt可用于MvpXml项目中的.NET。
答案 2 :(得分:0)
XSLT可以帮助进行排序,但对日期没有任何好处。 AFAIK,最简单的解决方法是从你拥有的日期字段生成一个带有“CCYYMMDD”(世纪,年,月,日)的字符串,并在选择时按字母顺序排序。输出时使用文本'as is'。
&lt; xsl:sort ...&gt;的组合和substring(datefield,xx,2)+ substring ...
希望这有帮助,