中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > XML/SOAP
在XSLT中实现按日期排序
作者:未知 时间:2004-02-21 12:12 出处:互联网 责编:chinaitpower
              摘要:暂无

我们在使用XSLT进行XML转换的时候,经常遇到按XML日期类型的数据进行排序的情况,按照默认的排序规则,很难实现正确的排序效果。虽然最新的MsXML3 SP4提供了3种数据类型的排序方式:

<xsl:sort select = string-expression data-type = { "text" | "number" | Qname } order = { "ascending" | "descending" } /> 但好像仍不能满足我们的需要,下面我们就介绍日期类型数据的排序方法。 我们的排序方法是基于下面语句的返回值为true的理论的: <script language="JavaScript"> alert(Date.parse("2004/08/09")==Date.parse("2004/8/9")) </script> 好了,下面就是我们的xsl文件:

SortXML.xsl

<?xml version="1.0" encoding="gb2312"?> <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"> <msxsl:script language="JavaScript" implements-prefix="user"> function xmlDateTime(nodelist) { return Date.parse(nodelist.replace(/-/g,"/")); } </msxsl:script> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="/"> <xsl:call-template name="ItemList"/> </xsl:template> <xsl:template name="ItemList"> <table bgcolor="snow" border="1" cellpadding="5" cellspacing="2" borderColor="darkorange" style="font-size:9pt"> <thead> <tr bgcolor=""> <th width="50%">标题</th> <th width="50%">修改时间</th> </tr> </thead> <tbody> <xsl:for-each select="/multistatus/response"> <xsl:sort order="descending" select="user:xmlDateTime(string(getlastmodified))" data-type="number"/> <tr> <td> <a href="{href}" target="_blank"> <xsl:value-of select="Title"/> </a> </td> <td> <xsl:value-of select="getlastmodified"/> </td> </tr> </xsl:for-each> </tbody> </table> </xsl:template> </xsl:stylesheet>

SortXML.xml

<?xml version="1.0" encoding="GB2312"?> <?xml-stylesheet type="text/xsl" href="SortXML.xsl"?> <multistatus> <response> <href>http://sz.luohuedu.net/xml/</href> <getlastmodified>2004-8-14 10:51:44</getlastmodified> <Title>【孟宪会之精彩世界】</Title> </response> <response> <href>http://dotnet.aspx.cc/Play.aspx</href> <getlastmodified>2004-10-23 11:11:10</getlastmodified> <Title>【孟宪会之精彩世界】音乐频道</Title> </response> <response> <href>http://dotnet.aspx.cc/</href> <getlastmodified>2004-02-10 18:36:19</getlastmodified> <Title>【孟宪会之精彩世界】</Title> </response> <response> <href>http://lucky.myrice.com/</href> <getlastmodified>2004-01-14 10:51:21</getlastmodified> <Title>【孟宪会之精彩世界】</Title> </response> <response> <href>http://dotnet.aspx.cc/ShowList.aspx&amp;id=1</href> <getlastmodified>2003-11-2 10:52:26</getlastmodified> <Title>【孟宪会之精彩世界】ASP.NET</Title> </response> <response> <href>http://dotnet.aspx.cc/CoolMenu/main.htm</href> <getlastmodified>1999-02-21 22:07:43</getlastmodified> <Title>【孟宪会之精彩世界】DHtml精彩放送</Title> </response> </multistatus>
关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有