DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> XML學習教程 >> XML詳解
在 XSLT 中正確地按日期排序
編輯:XML詳解     

用 XSLT 做 XML 轉換時,常會遇到需要依日期排序的需求。而預設的排序規則往往得不到我們期待的結果——原因是日期若存成 2004-8-14 10:51:44 這種字串形式,純文字比較會把「8」排在「10」後面,順序自然全亂。

即便是 MsXML3 SP4 提供了三種資料型別的排序方式:

<xsl:sort select="string-expression"
          data-type="{ "text" | "number" | Qname }"
          order="{ "ascending" | "descending" }" />

拿來排日期仍然不夠用。text 是字串比較,number 又無法直接吃日期字串。真正的解法是先把日期換算成可比較的數值,再進行排序。

核心思路

我們的做法建立在 JavaScript 的 Date.parse() 之上:把 2004-8-14 10:51:44 中的連字號替換成斜線,使其成為引擎認得的日期格式,parse 之後得到一個毫秒數。毫秒數是純數字,交给 data-type="number" 排序就完全不成問題了。

SortXML.xsl

<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt"
    xmlns:user="https://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">
    標題 修改時間
    <xsl:for-each select="/multistatus/response">
      <xsl:sort order="descending"
                select="user:xmlDateTime(string(getlastmodified))"
                data-type="number"/>
      <xsl:value-of select="Title"/>
      <xsl:value-of select="getlastmodified"/>
    </xsl:for-each>
  </xsl:template>

</xsl:stylesheet>

SortXML.xml

<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet type="text/xsl" href="SortXML.xsl"?>
<multistatus>
  <response>
    <href>https://sz.luohuedu.net/XML/</href>
    <getlastmodified>2004-8-14 10:51:44</getlastmodified>
    <Title>【孟憲會之精彩世界】</Title>
  </response>
  <response>
    <href>https://dotnet.aspx.cc/Play.aspx</href>
    <getlastmodified>2004-10-23 11:11:10</getlastmodified>
    <Title>【孟憲會之精彩世界】音樂頻道</Title>
  </response>
  <response>
    <href>https://dotnet.aspx.cc/</href>
    <getlastmodified>2004-02-10 18:36:19</getlastmodified>
    <Title>【孟憲會之精彩世界】</Title>
  </response>
  <response>
    <href>https://lucky.myrice.com/</href>
    <getlastmodified>2004-01-14 10:51:21</getlastmodified>
    <Title>【孟憲會之精彩世界】</Title>
  </response>
  <response>
    <href>https://dotnet.aspx.cc/ShowList.aspx?id=1</href>
    <getlastmodified>2003-11-2 10:52:26</getlastmodified>
    <Title>【孟憲會之精彩世界】ASP.Net</Title>
  </response>
  <response>
    <href>https://dotnet.aspx.cc/CoolMenu/main.htm</href>
    <getlastmodified>1999-02-21 22:07:43</getlastmodified>
    <Title>【孟憲會之精彩世界】DHTML 精彩放送</Title>
  </response>
</multistatus>

要留意的細節

exclude-result-prefixes 別漏:既然用了 msxsl 與 user 兩個非標準命名空間,就要在樣式表根元素上排除它們,否則輸出結果會沾上 xmlns: 宣告。
string() 是必要的一層:傳進腳本的是節點集,不轉成字串就無法呼叫 replace。
跨平台限制:msxsl:script 屬微軟專屬擴充,僅在 MSXML 上有效。若日後要移植到其他平台,正統做法是改用 XSLT 2.0 的 xs:dateTime 型別排序,或在資料進入 XSLT 之前就先把日期正規化成 ISO 格式。

其實最後一點才是根本解法:只要上游把日期統一存成 2004-08-14T10:51:44 這種 ISO 8601 形式,由於其字典序恰好等於時間序,用預設的文字排序就能得到正確結果,一行腳本都不必寫。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved