<script type="text/javascript">
</script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
在XSLT实现按日期排序作者:孟宪会 出自:【
孟宪会之精彩世界】 发布日期:2004年2月21日 11点47分56秒
我们在使用XSLT进行XML转换的时候,经常遇到按XML日期类型的数据进行排序的情况,按照默认的排序规则,很难实现正确的排序效果。虽然最新的MsXML3 SP4提供了3种数据类型的排序方式:
&lt;xsl:sort select = string-expression data-type = { "text" | "number" | Qname } order = { "ascending" | "descending" } /&gt;但好像仍不能满足我们的需要,下面我们就介绍日期类型数据的排序方法。我们的排序方法是基于下面语句的返回值为true的理论的:
好了,下面就是我们的xsl文件:
SortXML.xsl
&lt;?xml version="1.0" encoding="gb2312"?&gt;&lt;xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://dotnet.aspx.cc/" exclude-result-prefixes="msxsl user"&gt; &lt;msxsl:script language="JavaScript" implements-prefix="user"&gt; function xmlDateTime(nodelist) { return Date.parse(nodelist.replace(/-/g,"/")); } &lt;/msxsl:script&gt; &lt;xsl:output omit-xml-declaration="yes"/&gt; &lt;xsl:template match="/"&gt; &lt;xsl:call-template name="ItemList"/&gt; &lt;/xsl:template&gt; &lt;xsl:template name="ItemList"&gt; &lt;thead&gt; &lt;th width="50%"&gt;标题&lt;/th&gt; &lt;th width="50%"&gt;修改时间&lt;/th&gt; &lt;/thead&gt; &lt;xsl:for-each select="/multistatus/response"&gt; &lt;xsl:sort order="descending" select="user:xmlDateTime(string(getlastmodified))" data-type="number"/&gt; &lt;a href="{href}" rel="nofollow" target="_blank"&gt; &lt;xsl:value-of select="Title"/&gt; &lt;/a&gt; &lt;xsl:value-of select="getlastmodified"/&gt; &lt;/xsl:for-each&gt; &lt;/xsl:template&gt;&lt;/xsl:stylesheet&gt;
SortXML.xml
&lt;?xml version="1.0" encoding="GB2312"?&gt;&lt;?xml-stylesheet type="text/xsl" href="SortXML.xsl"?&gt;&lt;multistatus&gt; &lt;response&gt; &lt;href&gt;http://sz.luohuedu.net/xml/&lt;/href&gt; &lt;getlastmodified&gt;2004-8-14 10:51:44&lt;/getlastmodified&gt; &lt;Title&gt;【&lt;A href="http://www.66of.com" target=_blank&gt;孟宪会之精彩世界&lt;/A&gt;】&lt;/Title&gt; &lt;/response&gt; &lt;response&gt; &lt;href&gt;http://dotnet.aspx.cc/Play.aspx&lt;/href&gt; &lt;getlastmodified&gt;2004-10-23 11:11:10&lt;/getlastmodified&gt; &lt;Title&gt;【&lt;A href="http://www.66of.com" target=_blank&gt;孟宪会之精彩世界&lt;/A&gt;】音乐频道&lt;/Title&gt; &lt;/response&gt; &lt;response&gt; &lt;href&gt;http://dotnet.aspx.cc/&lt;/href&gt; &lt;getlastmodified&gt;2004-02-10 18:36:19&lt;/getlastmodified&gt; &lt;Title&gt;【&lt;A href="http://www.66of.com" target=_blank&gt;孟宪会之精彩世界&lt;/A&gt;】&lt;/Title&gt; &lt;/response&gt; &lt;response&gt; &lt;href&gt;http://lucky.myrice.com/&lt;/href&gt; &lt;getlastmodified&gt;2004-01-14 10:51:21&lt;/getlastmodified&gt; &lt;Title&gt;【&lt;A href="http://www.66of.com" target=_blank&gt;孟宪会之精彩世界&lt;/A&gt;】&lt;/Title&gt; &lt;/response&gt; &lt;response&gt; &lt;href&gt;http://dotnet.aspx.cc/ShowList.aspx&amp;amp;id=1&lt;/href&gt; &lt;getlastmodified&gt;2003-11-2 10:52:26&lt;/getlastmodified&gt; &lt;Title&gt;【&lt;A href="http://www.66of.com" target=_blank&gt;孟宪会之精彩世界&lt;/A&gt;】ASP.NET&lt;/Title&gt; &lt;/response&gt; &lt;response&gt; &lt;href&gt;http://dotnet.aspx.cc/CoolMenu/main.htm&lt;/href&gt; &lt;getlastmodified&gt;1999-02-21 22:07:43&lt;/getlastmodified&gt; &lt;Title&gt;【&lt;A href="http://www.66of.com" target=_blank&gt;孟宪会之精彩世界&lt;/A&gt;】&lt;A href="http://www.66of.com" target=_blank&gt;DHtml&lt;/A&gt;&lt;A href="http://www.66of.com" target=_blank&gt;精彩放送&lt;/A&gt;&lt;/Title&gt; &lt;/response&gt;&lt;/multistatus&gt;
http://sz.luohuedu.net/xml/SortXML.xml
<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
<script type="text/javascript">
</script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>