• λ我爱Aspx >> Asp.Net >> 在XSLT中实现按日期排序
  • 在XSLT中实现按日期排序

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

    <xsl:sort select = string-expression data-type = &#123; "text" | "number" | qname &#125; order = &#123; "ascending" | "descending" &#125; /> 但好像仍不能满足我们的需要,下面我们就介绍日期类型数据的排序方法。 我们的排序方法是基于下面语句的返回值为true的理论的: JavaScript"> alert(Date.parse("2004/08/09")==Date.parse("2004/8/9")) 好了,下面就是我们的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) &#123; return Date.parse(nodelist.replace(/-/g,"/")); &#125; </msxsl:script> <xsl:output omit-xml-declaration="yes"/> <xsl:template match="/"> <xsl:call-template name="itemlist"/> </xsl:template> <xsl:template name="itemlist"> <thead> <tr> <th>标题</th> <th>修改时间</th> </tr> </thead> <tbody> <xsl:for-each select="/multistatus/response"> <xsl:sort order="descending" select="user:xmldatetime(string(getlastmodified))" data-type="number"/> <tr> <td> <xsl:value-of select="title"/> </td> <td> <xsl:value-of select="getlastmodified"/> </td> </tr> </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>

    上一篇: 使用.NET存储XML数据

    下一篇: 在XSL中引用自身数据的两种方法

    Ҷƪл˵?
  • һƪ在XSL中引用自身数据的两种方法
    һƪXslTransform.Transform 方法如何将结果输出到字符串里?