在XSLT实现按日期排序
来源:互联网 发布:seo 中文url 编辑:程序博客网 时间:2024/05/16 05:43
在XSLT实现按日期排序
作者:孟宪会 出自:【孟宪会之精彩世界】 发布日期:2004年2月21日 11点47分56秒
我们在使用XSLT进行XML转换的时候,经常遇到按XML日期类型的数据进行排序的情况,按照默认的排序规则,很难实现正确的排序效果。虽然最新的MsXML3 SP4提供了3种数据类型的排序方式:
<xsl:sort
select = string-expression
data-type = { "text" | "number" | Qname }
order = { "ascending" | "descending" }
/>
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>
<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:44getlastmodified>
<Title>【孟宪会之精彩世界】Title>
response>
<response>
<href>http://dotnet.aspx.cc/Play.aspxhref>
<getlastmodified>2004-10-23 11:11:10getlastmodified>
<Title>【孟宪会之精彩世界】音乐频道Title>
response>
<response>
<href>http://dotnet.aspx.cc/href>
<getlastmodified>2004-02-10 18:36:19getlastmodified>
<Title>【孟宪会之精彩世界】Title>
response>
<response>
<href>http://lucky.myrice.com/href>
<getlastmodified>2004-01-14 10:51:21getlastmodified>
<Title>【孟宪会之精彩世界】Title>
response>
<response>
<href>http://dotnet.aspx.cc/ShowList.aspx&id=1href>
<getlastmodified>2003-11-2 10:52:26getlastmodified>
<Title>【孟宪会之精彩世界】ASP.NETTitle>
response>
<response>
<href>http://dotnet.aspx.cc/CoolMenu/main.htmhref>
<getlastmodified>1999-02-21 22:07:43getlastmodified>
<Title>【孟宪会之精彩世界】DHtml精彩放送Title>
response>
multistatus>
xml-stylesheet type="text/xsl" href="SortXML.xsl"?>
<multistatus>
<response>
<href>http://sz.luohuedu.net/xml/href>
<getlastmodified>2004-8-14 10:51:44getlastmodified>
<Title>【孟宪会之精彩世界】Title>
response>
<response>
<href>http://dotnet.aspx.cc/Play.aspxhref>
<getlastmodified>2004-10-23 11:11:10getlastmodified>
<Title>【孟宪会之精彩世界】音乐频道Title>
response>
<response>
<href>http://dotnet.aspx.cc/href>
<getlastmodified>2004-02-10 18:36:19getlastmodified>
<Title>【孟宪会之精彩世界】Title>
response>
<response>
<href>http://lucky.myrice.com/href>
<getlastmodified>2004-01-14 10:51:21getlastmodified>
<Title>【孟宪会之精彩世界】Title>
response>
<response>
<href>http://dotnet.aspx.cc/ShowList.aspx&id=1href>
<getlastmodified>2003-11-2 10:52:26getlastmodified>
<Title>【孟宪会之精彩世界】ASP.NETTitle>
response>
<response>
<href>http://dotnet.aspx.cc/CoolMenu/main.htmhref>
<getlastmodified>1999-02-21 22:07:43getlastmodified>
<Title>【孟宪会之精彩世界】DHtml精彩放送Title>
response>
multistatus>
- 在XSLT实现按日期排序
- 实现按日期和大小排序 Android
- 在批处理文件中实现按日期命名的目录迁移
- 在 XSL/XSLT 中实现随机排序
- 在 XSL/XSLT 中实现随机排序
- 在 XSL/XSLT 中实现随机排序
- 在 XSL/XSLT 中实现随机排序
- 在XSL/XSLT中实现随机排序
- 在XSL/XSLT中实现随机排序
- java API提供的方法实现排序 如file按日期和大小排序
- java 取目录文件按日期排序
- fckEditor显示文件按日期时间排序
- java 取目录文件按日期排序
- js的数组按日期排序
- 查询数据按日期排序 先排日期有值的 在排为null的
- mysql 按月份排序,如果月份相同,按日期排序
- 一个按日期排序输出的查询方法
- mysql获取按日期排序获取最新的记录
- Cache浏览器
- 如何控制DataGrid里的内容换行与不换行?
- 使用ADOX创建Access数据库和表
- 在ASP.NET中显示进度条
- 将某一目录下的所有相同格式的 XML文件绑定到不同的DataGrid
- 在XSLT实现按日期排序
- XP 风格的可拖动列、可排序、可改变宽度的DataGrid的例子
- 实现类似Windows资源管理器的DataGrid
- 在后代码里创建DataGrid控件
- 创建可拖动列的DataGrid(2)
- 创建可拖动列的DataGrid
- 编写随Windows启动自动运行的程序
- 把图象文件转换成XML格式文件
- 在.NET应用程序中添加拼写和语法检查功能