动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
   Xml → 阅读文章

 在XSLT实现按日期排序

作者来源: 
阅读 1423 人次 , 2006-3-29 3:29:00 

我们在使用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="utf-8"?> <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> </thead> <tbody> <xsl:for-each select="/multistatus/response"> <xsl:sort order="descending" select="user:xmlDateTime(string(getlastmodified))" data-type="number"/> <a href="{href}" target="_blank"> <xsl:value-of select="Title"/> </a> <xsl:value-of select="getlastmodified"/> </xsl:for-each> </tbody> </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>

查看转换结果

http://sz.luohuedu.net/xml/SortXML.xml

  
 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:在XSL里执行类似for(i=0;i
· 下一篇:
开发基于XML的留言板
· XML数据库中几个容易混淆的概念
· 用ASP、VB和XML建立互联网应用程序(4)
· 在.NET Framework中轻松处理XML数据(4-1)
· 问:如何将 XML 文件导入 SQL Server 2000?
· 掌握SAX


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077