在Web開發(fā)中,我們經(jīng)常會遇到分頁顯示和排序數(shù)據(jù)記錄集的情況,這在服務器端使用服務器端的代碼和數(shù)據(jù)庫技術(shù)是件很輕松的事情,比如:ASP、PHP、JSP等。然而,如果要在客戶端顯示多條記錄并且排序是一件很令人頭疼的事情。下面,我們利用Extensible Markup Language(XML,可擴展標記語言)和Extensible Stylesheet Language Transformations(XSLT,可擴展樣式單語言轉(zhuǎn)換),并結(jié)合XML Path Language(XPath,XML路徑語言),只需要編寫簡單的代碼,就可輕松實現(xiàn)。這種方法避免了與服務器頻繁打交道的過程,節(jié)省了數(shù)據(jù)顯示的時間,瀏覽者無須等待就可以看到結(jié)果,也可以減少服務器的負擔。另外。由于XML和XSLT技術(shù),使數(shù)據(jù)存儲和數(shù)據(jù)顯示分離,還可以讓我們的代碼能夠重復利用,大大減輕了程序員編寫代碼的負擔。
下面,我們一步一步地來實現(xiàn)我們的功能。
首先:創(chuàng)建XSLT
XSLT樣式單的第一行標明該XML所遵照的XML規(guī)范版本,然后是標明該樣式單使用的名稱空間,這里,我們以XSL規(guī)范的正式版本來進行編寫,而不采用XSL的草案的寫法:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
注意:兩者在功能和寫法上有很大的差異。
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
接下來,我們定義XSLT里的模板標記:
<xsl:template match="/">
<xsl:apply-templates select="/客戶關系表"/>
</xsl:template>
<xsl:template match="/客戶關系表"></xsl:template>
我們把要顯示的樣式寫到模板里面。我們使用HTML的數(shù)據(jù)島來存放我們的數(shù)據(jù),這些數(shù)據(jù)可以利用SQL Server 2000的XML查詢來得到,對于不支持XML的數(shù)據(jù)庫,我們可以自己編寫組件把數(shù)據(jù)轉(zhuǎn)換成XML格式,然后在放到數(shù)據(jù)島里。在HTML里使用數(shù)據(jù)島有兩種方法:
一是直接嵌入數(shù)據(jù),如下所示:
<XML id=‘‘Data‘‘>
<客戶關系表>
<客戶>每條數(shù)據(jù)</客戶>
</客戶關系表>
</XML>
二是通過SRC屬性引用外部文件,如下所示:
<XML id=‘‘Data‘‘ src=‘‘Data.xml‘‘></XML>
要使用數(shù)據(jù)島里的數(shù)據(jù),必須通過id名來引用它,當然,由于XSLT文件也是XML格式文件的一種,也可以通過這種方法來實現(xiàn):
<XML id=‘‘Style‘‘ src=‘‘Style.xsl‘‘></XML>
我們在頁面中加入標記DIV來顯示我們的轉(zhuǎn)換的結(jié)果:
<div id="DisplayArea"></div>
使用XSLT轉(zhuǎn)換數(shù)據(jù)島里的數(shù)據(jù),采用DOMDocument的transNode()方法,并把結(jié)果通過DIV的innerHTML屬性來展現(xiàn)出來:
DisplayArea.innerHTML = Data.transformNode(Style.DocumentElement)
第二步:實現(xiàn)客戶端排序的功能
我們先設定一個默認的排序字段,這里選擇“序號”作為默認的排序關鍵字,并且是按遞增的順序排列,在XSLT里加入sort標記:
<xsl:for-each select="客戶">
<xsl:sort select="序號" order="descending" data-type="number"/>
</xsl:for-each>
接下來,我們?yōu)闃邮奖碓黾优判虻墓δ?,以便可以響應用戶的操作,我們在表頭的每個列上添加onClick事件,該事件調(diào)用sort()函數(shù),允許用戶通過單擊該表頭來進行對該列的排序。
<td onClick="sort(‘‘序號‘‘)">序號</td>
Sort()函數(shù)的語句如下所示:
Function Sort(strField)
Dim sortField
Dim sortOrderAttribute
‘‘ 得到原來排序字段的屬性值
Set sortField = Style.XMLDocument.selectSingleNode("http://xsl:sort/@select")
‘‘ 得到原來排序的順序?qū)傩灾?nbsp;
Set sortOrderAttribute = Style.XMLDocument.selectSingleNode("http://xsl:sort/@order")
‘‘ 如果我們已經(jīng)按所點擊的列的字段排序,我們必須改變排序的順序;
‘‘ 否則,我們只需要按新所點擊的列字段按默認的順序進行排序
If sortField.Value = strField Or sortField.Value = "./*[0]" Then
If sortOrderAttribute.Value = "descending" Then
sortOrderAttribute.Value = "ascending"
Else
sortOrderAttribute.Value = "descending"
End If
Else
sortField.Value = strField
sortOrderAttribute.Value = "ascending"
End If
Set sortField = Nothing
Set sortOrderAttribute = Nothing
‘‘ 輸出排序后的結(jié)果
DisplayArea.innerHTML = Data.transformNode(Style.DocumentElement)
End Function
下面,我們實現(xiàn)每頁面顯示的記錄數(shù)和設定前頁、后頁的功能。使用span標記顯示目前顯示的是第幾頁、共多少頁和記錄的總數(shù)。我們默認每頁顯示6條記錄,用變量intRecordsPerPage保存該值:
<table width="100%" border="0" style="font-size:9pt">
<tr>
<td align="left"><b>第 <span id="CurrentPage"></span> 頁 總 <span id="PageCount"></span> 頁 共有 <span id="RecordCount"></span> 條記錄</b></td>
<td align="right"><b>每頁記錄數(shù):<input onblur="setRecordsPerPage()" id="RecordsPerPage" style="vertical-align:middle;height:15pt;width:30px"/></b></td>
<td align="right">
<span id="Paging">
<input type="button" OnClick="FirstPage()" value="第一頁"/>
<input type="button" OnClick="previousPage(1)" value="上一頁"/>
<input type="button" OnClick="nextPage(1)" value="下一頁"/>
<input type="button" OnClick="LastPage()" value="最末頁"/>
</span>
</td>
</tr>
</table>
下面以“下一頁”按鈕執(zhí)行的操作為例子,說明轉(zhuǎn)換不同頁面的處理過程。該函數(shù)根據(jù)參數(shù)intPage來決定要顯示的記錄的條數(shù)和相應的頁面,每個按鈕Value值的變化是通過動態(tài)改變XSL DOM的內(nèi)容來實現(xiàn)的:
Function nextPage(intPage)
Dim strDisplay
Dim strDateRange
If CInt(CStr(intPage) * intRecordsPerPage) < _
Data.selectNodes("/客戶關系表/客戶").length Then
intPage = CInt(intPage) + 1
Style.XMLDocument.selectNodes("http://@OnClick") _
(1).Value = "previousPage(" & intPage & ")"
Style.XMLDocument.selectNodes("http://@OnClick") _
(2).Value = "nextPage(" & intPage & ")"
Style.XMLDocument.selectNodes _
("http://xsl:for-each/@select")(1).Value = _
"./客戶[position() <= " & (CStr(intPage) _
* intRecordsPerPage) & " and position() > " _
& (CInt(intPage) - 1) * intRecordsPerPage & _
"]"
redisplay (intPage)
End If
End Function
下面,我們來看看設置每個頁面記錄條數(shù)的函數(shù)setRecordsPerPage(),該函數(shù)通過動態(tài)修改xsl:for-each的select屬性值來實現(xiàn)的,使用XPath來遍歷那些符合節(jié)點位置大于0并且節(jié)點位置小于每頁記錄數(shù)加1的那些節(jié)點。其中主要的語句是下面的一行:
Style.XMLDocument.selectNodes("http://xsl:for-each/@select")(1). _
value = "./客戶[position() < " & intRecordsPerPage + 1 & " and "& " position() > 0]"
到目前為止,我們的頁面既可以實現(xiàn)排序,也實現(xiàn)動態(tài)改變每頁顯示記錄條數(shù)的功能了,為了實現(xiàn)可重用的要求,我們還可以進行進一步的改進。XPath是進行XML/XSLT應用開發(fā)的一個強有力的工具,XPath中可以使用通配符,使XSLT樣式單文件完全不依賴于你的數(shù)據(jù)節(jié)點名稱。因此,我們在改變XML數(shù)據(jù)的時候,只要不改變節(jié)點的層次關系,可以不必改動XSLT就可以直接使用。比如:在本例中,你可以添加或者刪除某些字段、或添加刪除一些記錄,直接使用本例中的XSLT,不但可以在表格里正常顯示出數(shù)據(jù),而且還能正常排序和分頁。
下面我們就分析一下是如何實現(xiàn)的。比如下面的層次關系:
<客戶關系表>
<客戶>
<序號></序號>
<姓名></姓名>
<電子郵件></電子郵件>
</客戶>
</客戶關系表>
假如我們的XSLT中有這樣一個選擇模板的句子:
<xsl:apply-templates select="/客戶關系表"/>
為了實現(xiàn)通用性的要求,我們可以使用通配符:
<xsl:apply-templates select="/*"/>
這里我們使用了子運算符"/",它選擇了根下的所有節(jié)點,兩者的不同點在于:"/客戶關系表"選擇的是根下的客戶關系表子節(jié)點,而"/*"選擇的是根下所有的直接子節(jié)點,在上面的XML數(shù)據(jù)格式中,二者是完全等價的。
對于下面的for-each循環(huán)來說:
<xsl:for-each select="客戶">
<xsl:sort select="序號" order="ascending"/>
</xsl:for-each>
我們可以改變成這樣的形式:
<xsl:for-each select="./*">
<xsl:sort select="./*[1]" order="ascending"/>
</xsl:for-each>
這里"./*"表示你應當包含進去當前節(jié)點下所有的一級子節(jié)點,語法"./*[1]"表示的是選擇當前節(jié)點中的第一個子節(jié)點。
另外還有一個地方可以改進的是<xsl:value-of select="序號"/>,我們可以把它改成<xsl:value-of select="."/>,表示在每一次循環(huán)中選擇當前節(jié)點。
在我們的函數(shù)中,還使用了一些硬代碼,如果不做改動的話,我們的通用性還是實現(xiàn)不了,因此,我們下面就看看如何替換硬代碼中的語句。
在創(chuàng)建表頭的時候,我們使用了<td onClick="sort(‘‘序號‘‘)"> 序號</td>的語句,如果XML數(shù)據(jù)里沒有序號節(jié)點的話,這里顯然會出現(xiàn)錯誤的,為了實現(xiàn)通用性,我們自定義了一個函數(shù)getName,來取得所要顯示的節(jié)點的名稱:
<td>
<xsl:attribute name="onClick">
Sort(‘‘<xsl:value-of select="user:getName(.)"/>‘‘)
</xsl:attribute>
<xsl:value-of select="user:getName(.)"/>
</td>
自定義函數(shù)是XSLT的一個突出的功能,要使用這個特性,我們得用msxml:script元素來定義,同時,必須在樣式單定義的時候指定一個用戶定義的名字空間。下面就是我們使用自定義函數(shù)的全部內(nèi)容:
<xsl:stylesheet xmlns:xsl=http://www.w3.org/1999/XSL/Transform
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="http://lucky.myrice.com"
version="1.0">
<msxsl:script language="VBScript" implements-prefix="user">
<![CDATA[
function getName(node)
getName = node.item(0).nodeName
end function
}>
</msxsl:script>
在我們的XSLT文件中,使用了兩個循環(huán),我們分別進行相應的更改,第一處:顯示表頭的地方改為<xsl:for-each select="./*[1]/*">,它等同于<xsl:for-each select="客戶關系表/客戶[1]/*">;第二處循環(huán)是顯示每行記錄,改成<xsl:for-each select="./*">。還有其他的地方需要更改的,請參見后面的完整源代碼部分。這樣我們就完成了通用的XSLT文件,不管你的XML數(shù)據(jù)有多少字段,也不管節(jié)點名稱是什么,我們都無需更改XSLT文件,就可以實現(xiàn)我們的功能了。最終的瀏覽效果將會象下圖所示:
以下是完整的Style.xsl文件的內(nèi)容:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:user="http://lucky.myrice.com" version="1.0">
<msxsl:script language="VBScript" implements-prefix="user">
<![CDATA[
Function getName(node)
getName = node.Item(0).nodeName
End Function
}>
</msxsl:script>
<xsl:template match="/">
<xsl:apply-templates select="/*"/>
</xsl:template>
<xsl:template match="/*">
<table width="100%" border="0" style="font-size:9pt">
<tr>
<td align="left"><b>第 <span id="CurrentPage"></span> 頁 總 <span id="PageCount"></span> 頁 共有 <span id="RecordCount"></span> 條記錄</b></td>
<td align="right"><b>每頁記錄數(shù):<input onblur="setRecordsPerPage()" id="RecordsPerPage" style="vertical-align:middle;height:15pt;width:30px"/></b></td>
<td align="right">
<span id="Paging">
<input type="button" OnClick="FirstPage()" value="第一頁"/>
<input type="button" OnClick="previousPage(1)" value="上一頁"/>
<input type="button" OnClick="nextPage(1)" value="下一頁"/>
<input type="button" OnClick="LastPage()" value="最末頁"/>
</span>
</td>
</tr>
</table>
<Table WIDTH="100%" BORDER="0" cellpadding="0" cellspacing="1" style="font-size:11pt" bgcolor="#0099ff">
<tr bgcolor="#FF6600" style="cursor: hand;padding:5px">
<xsl:for-each select="./*[1]/*">
<td align="center">
<xsl:attribute name="onclick">
Sort(‘‘<xsl:value-of select="user:getName(.)"/>‘‘)
</xsl:attribute>
<font color="#EEEEEE"><b><u><xsl:value-of select="user:getName(.)"/></u></b></font>
</td>
</xsl:for-each>
</tr>
<xsl:for-each select="./*[position() < 6 and position() > 0]">
<xsl:sort select="./*[1]" order="ascending"/>
<tr bgcolor="#FFCCFF">
<xsl:for-each select="./*">
<td> <xsl:value-of select="."/></td>
</xsl:for-each>
</tr>
</xsl:for-each>
</Table>
</xsl:template>
</xsl:stylesheet>
以下是進行輸出的Exam.htm文件:
<HTML>
<Head>
<META http=equiv="Content-Type" Content="text/html;charset=gb2312">
<STYLE>
body { font-family:宋體; font-size:9pt;}
th { font-family:宋體; font-size:11pt; font-weight:bold;}
</STYLE>
<Script language="vbscript">
Option Explicit
Dim intRecordsPerPage ‘‘每個頁面顯示的記錄數(shù)
intRecordsPerPage = 6 ‘‘每個頁面顯示的記錄數(shù),默認設定為6
‘‘ 更新顯示頁面的函數(shù)
Function window_onload()
‘‘ 顯示設定的記錄數(shù)
Style.XMLDocument.selectNodes("http://xsl:for-each/@select")(1).Value = "./*[position() < " & intRecordsPerPage + 1 & " and position() > 0]"
transform()
setPageCount()
End Function
‘‘ 進行XML-XSLT轉(zhuǎn)換,并顯示當前記錄的一些信息
Function transform()
DisplayArea.innerHTML = Data.transformNode(Style.DocumentElement)
RecordsPerPage.Value = intRecordsPerPage
End Function
‘‘ 重新轉(zhuǎn)換XML,并顯示一個狀態(tài)信息
Function redisplay(intPage)
Dim strDisplay
Dim intPageCount
Dim intRecordCount
‘‘ 保存狀態(tài)信息
intPageCount = PageCount.innerHTML
intRecordCount = RecordCount.innerHTML
transform()
‘‘ 顯示狀態(tài)信息
PageCount.innerHTML = intPageCount
RecordCount.innerHTML = intRecordCount
CurrentPage.innerHTML = intPage
End Function
‘‘ 重新排序和顯示
Function Sort(strField)
Dim sortField
Dim sortOrderAttribute
‘‘ 得到排序?qū)傩灾?nbsp;
Set sortField = Style.XMLDocument.selectSingleNode("http://xsl:sort/@select")
Set sortOrderAttribute = Style.XMLDocument.selectSingleNode("http://xsl:sort/@order")
‘‘ 改變排序的方式
If sortField.Value = strField Or sortField.Value = "./*[0]" Then
If sortOrderAttribute.Value = "descending" Then
sortOrderAttribute.Value = "ascending"
Else
sortOrderAttribute.Value = "descending"
End If
Else
sortField.Value = strField
sortOrderAttribute.Value = "ascending"
End If
Set sortField = Nothing
Set sortOrderAttribute = Nothing
redisplay (CurrentPage.innerHTML)
End Function
‘‘ 重新設置每頁的記錄數(shù)
Function setRecordsPerPage()
If IsNumeric(RecordsPerPage.Value) Then
intRecordsPerPage = CInt(RecordsPerPage.Value)
window_onload
End If
End Function
‘‘ 顯示頁數(shù)信息
Function setPageCount()
Dim intTotalRecords
PageCount.innerHTML = getNumberOfPages(intTotalRecords)
RecordCount.innerHTML = intTotalRecords
CurrentPage.innerHTML = 1
End Function
‘‘ 計算總頁數(shù)和總記錄數(shù)
Function getNumberOfPages(intTotalRecords)
Dim intPages
intTotalRecords = Data.XMLDocument.selectNodes("/*/*").length
intPages = intTotalRecords / intRecordsPerPage
If InStr(intPages, ".") > 0 Then
intPages = CInt(Left(intPages, InStr(intPages, "."))) + 1
End If
getNumberOfPages = intPages
End Function
‘‘ “下一頁”的處理
Function nextPage(intPage)
Dim strDisplay
Dim strDateRange
If CInt(CStr(intPage) * intRecordsPerPage) < Data.selectNodes("/*/*").length Then
intPage = CInt(intPage) + 1
Style.XMLDocument.selectNodes("http://@OnClick")(1).Value = "previousPage(" & intPage & ")"
Style.XMLDocument.selectNodes("http://@OnClick")(2).Value = "nextPage(" & intPage & ")"
Style.XMLDocument.selectNodes("http://xsl:for-each/@select")(1).Value = "./*[position() <= " & (CStr(intPage) * intRecordsPerPage) & " and position() > " & (CInt(intPage) - 1) * intRecordsPerPage & "]"
redisplay (intPage)
End If
End Function
‘‘ 處理“上一頁”
Function previousPage(intPage)
Dim strDisplay
Dim strDateRange
If intPage > 1 Then
intPage = CInt(intPage) - 1
Style.XMLDocument.selectNodes("http://@OnClick")(1).Value = "previousPage(" & intPage & ")"
Style.XMLDocument.selectNodes("http://@OnClick")(2).Value = "nextPage(" & intPage & ")"
Style.XMLDocument.selectNodes("http://xsl:for-each/@select")(1).Value = "./*[position() <= " & (CStr(intPage) * intRecordsPerPage) & " and position() > " & (CInt(intPage) - 1) * intRecordsPerPage & "]"
redisplay (intPage)
End If
End Function
‘‘ “第一頁”的處理
Function FirstPage()
Style.XMLDocument.selectNodes("http://@OnClick")(1).Value = "previousPage(1)"
Style.XMLDocument.selectNodes("http://@OnClick")(2).Value = "nextPage(1)"
Style.XMLDocument.selectNodes("http://xsl:for-each/@select")(1).Value = "./*[position() < " & intRecordsPerPage + 1 & " and position() > 0]"
transform()
setPageCount()
End Function
‘‘ “最末頁”的處理
Function LastPage()
Dim intTotalPages
Dim intTotalRecords
intTotalPages = getNumberOfPages(intTotalRecords)
nextPage (intTotalPages - 1)
End Function
</Script>
</Head>
<body>
<p align="center" style="font-weight:bold;font-size:12pt;color:#0000FF;border-bottom:3px double red;padding-bottom:5px">客戶關系表</p>
<XML id=‘‘Data‘‘>
<客戶關系表 xmlns:dt="urn:schemas-microsoft-com:datatypes">
<客戶><序號 dt:dt="int">01</序號><姓名>Mi</姓名><電子郵件>water@21cn.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">02</序號><姓名>uyi</姓名><電子郵件>Lily@sina.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">03</序號><姓名>uiyu</姓名><電子郵件>John@21cn.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">04</序號><姓名>Doug</姓名><電子郵件>Karry@163.net</電子郵件></客戶>
<客戶><序號 dt:dt="int">05</序號><姓名>Ellen</姓名><電子郵件>vivki@sina.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">06</序號><姓名>Frank</姓名><電子郵件>net_lover@mengxianhui.com.cn</電子郵件></客戶>
<客戶><序號 dt:dt="int">07</序號><姓名>Greg</姓名><電子郵件>meng@mengxianhui.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">08</序號><姓名>Harry</姓名><電子郵件>sunny@xianhui.net</電子郵件></客戶>
<客戶><序號 dt:dt="int">09</序號><姓名>Ingrid</姓名><電子郵件>cathy@hotmail.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">10</序號><姓名>Jeff</姓名><電子郵件>your@mxh.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">11</序號><姓名>Kelly</姓名><電子郵件>Iloveyou@mengxianhui.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">12</序號><姓名>Larry</姓名><電子郵件>smilling@mengxianhui.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">13</序號><姓名>Mark</姓名><電子郵件>money@21cn.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">14</序號><姓名>Nancy</姓名><電子郵件>www@yahoo.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">15</序號><姓名>Peter</姓名><電子郵件>dotnet@aol.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">16</序號><姓名>Rachel</姓名><電子郵件>billgates@microsoft.com</電子郵件></客戶>
<客戶><序號 dt:dt="int">17</序號><姓名>Seth</姓名><電子郵件>flying@yous.net</電子郵件></客戶>
<客戶><序號 dt:dt="int">18</序號><姓名>Tim</姓名><電子郵件>agooyboy@lovegirl.com</電子郵件></客戶>
</客戶關系表>
</XML>
<XML id=‘‘Style‘‘ src=‘‘Style.xsl‘‘></XML>
<div id="DisplayArea"></div>
<table border="0" width="100%" style="font-size:11pt;">
<tr>
<td align="right">資料來源:【<a >孟憲會之精彩世界</a>】</td>
</tr>
</table>
</body>
</HTML>
把上面的內(nèi)容拷貝到本地計算機上,分別保存為相應的文件,在IE5+和XML3.0+的環(huán)境下即可看到效果!