查詢功能是我們在網站上見過的最普遍也是最常用的一個功能模塊了。以往的信息查詢都是連接到數據庫的,每一次點擊都必須要后臺數據庫的支持。然而很多情況下用戶往往只針對某一部分的數據進行操作,這樣不但服務器的負擔加重,而且嚴重的影響用戶瀏覽的速度。
針對這種情況我們需要將用戶需要的某一部分數據以XML的方式傳遞到客戶端,用戶對這些數據可以很方便的進行操作。既方便了用戶,又減輕了服務器數據庫的負擔。何樂而不為呢!而且這項功能可以通用到其他眾多模塊,因此添加了這個動態查詢功能。
材料:
XML卷之動態查詢
有2個文件:search.xml 和 search.xsl
作用:
在不刷新頁面的情況下對數據進行過濾篩選,有效的提高數據查詢的功能。
代碼:
以下為引用的內容: search.xml <?xml version="1.0" encoding="gb2312" ?> <?xml-stylesheet type="text/xsl" href="search.xsl" ?> <BlueIdea> <team> <blue_ID>1</blue_ID> <blue_name>Sailflying</blue_name> <blue_text>一個簡單的查詢</blue_text> <blue_time>2002-1-11 17:35:33</blue_time> <blue_class>XML專題</blue_class> </team> <team> <blue_ID>2</blue_ID> <blue_name>flyingbird</blue_name> <blue_text>嫁給你,是要你疼的</blue_text> <blue_time>2001-09-06 12:45:51</blue_time> <blue_class>灌水精華</blue_class> </team> <team> <blue_ID>3</blue_ID> <blue_name>苛子</blue_name> <blue_text>正則表達式在UBB論壇中的應用</blue_text> <blue_time>2001-11-23 21:02:16</blue_time> <blue_class>Web 編程精華</blue_class> </team> <team> <blue_ID>4</blue_ID> <blue_name>太乙郎</blue_name> <blue_text>年末經典分舵聚會完全手冊 v0.1</blue_text> <blue_time>2000-12-08 10:22:48</blue_time> <blue_class>論壇灌水區</blue_class> </team> <team> <blue_ID>5</blue_ID> <blue_name>mmkk</blue_name> <blue_text>Asp錯誤信息總匯</blue_text> <blue_time>2001-10-13 16:39:05</blue_time> <blue_class>javascript腳本</blue_class> </team> </BlueIdea> search.xsl <?xml version="1.0" encoding="gb2312" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <head> <title> XML卷之實戰錦囊(2):動態查詢</title> <style> body,BlueIdea,team,blue_ID,blue_name,blue_text,blue_time,blue_class{ font: 12px "宋體", "Arial", "Times New Roman"; } table { font-size: 12px; border: 0px double; border-color: #99CC99 #99CC99 #CCCCCC #CCCCCC; cellpadding:3;cellspacing:3; bgcolor:#eeeeee; text-decoration: blink} span { font-size: 12px; color: red; } </style> <script> function searchtext(x) { stylesheet=document.XSLDocument; source=document.XMLDocument; sortField=document.XSLDocument.selectNodes("//@select"); if (x!="") { sortField[1].value="team[blue_ID='" x "']"; Layer1.innerHTML=source.documentElement.transformNode(stylesheet); } else {alert("請輸入篩選條件!");} } </script> </head> <body> <p align="center"><span>XML卷之實戰錦囊(2):動態查詢</span></p> <div id="Layer1" name="Layer1"> <xsl:apply-templates select="BlueIdea" /> </div> <hr size="1" width="500" /> <table align="center" cellpadding="0" cellspacing="0" border="0" > <tr> <td> <span >請輸入篩選條件 : </span> blue_ID= <input type="text" name="searchtext" size="1" maxlength="1" /> <input type="button" class="button" onClick="searchtext(document.all.searchtext.value)" value="Search" name="button" /> </td> </tr> </table> </body> </html> </xsl:template> <xsl:template match="BlueIdea"> <table width="500" border="1" align="center" cellpadding="1" cellspacing="1" bordercolordark="#ffffff" bordercolorlight="#ADAAAD"> <tr bgcolor="#FFCC99" align="center"> <td>編號</td> <td>姓名</td> <td>主題</td> <td>發表時間</td> <td>歸類</td> </tr> <xsl:apply-templates select="team" order-by="blue_ID"/> </table> </xsl:template> <xsl:template match="team"> <tr align="center"> <xsl:apply-templates select="blue_ID" /> <xsl:apply-templates select="blue_name" /> <xsl:apply-templates select="blue_text" /> <xsl:apply-templates select="blue_time" /> <xsl:apply-templates select="blue_class" /> </tr> </xsl:template> <xsl:template match="blue_ID"> <td bgcolor="#eeeeee"> <xsl:value-of /> </td> </xsl:template> <xsl:template match="blue_name"> <td> <xsl:value-of /> </td> </xsl:template> <xsl:template match="blue_text"> <td> <xsl:value-of /> </td> </xsl:template> <xsl:template match="blue_time"> <td> <xsl:value-of /> </td> </xsl:template> <xsl:template match="blue_class"> <td> <xsl:value-of /> </td> </xsl:template> </xsl:stylesheet> |
講解:
1)search.xml 是數據文件,相信大家都不會有問題。
2)search.xsl 是格式文件,有幾個地方要注意。
(1)腳本中:
sortField=document.XSLDocument.selectNodes("//@select");
作用是:找到所有屬性為select的節點。這個和我在動態排序中說到的
sortField=document.XSLDocument.selectSingleNode("//@order-by");
有些不一樣了。大家注意這個小小的區別以及各自的功能。
sortField[1].value="team[blue_ID='" x "']";
因此sortField[1]就是找到的第二個節點,它對應的節點就是
<xsl:apply-templates select="team" order-by="blue_ID"/>
參數 x 是文本框中輸入的數值。
我們將select="team" 的搜索條件修改為select="team[blue_ID='x']"
作用是:增加判斷條件,只有blue_ID的數值等于 x 的XML數據才顯示出來。
當然大家可以豐富判斷的條件,我在這里做的簡單判斷是為了讓大家更容易理解。
最后通過重新顯示Layer1的innerHTML值來顯示新的排序內容。
(2)文本中
select="team"
在我這里它是 sortField[1],但你在做的時候可能就會更改。
那么你就一定要計算準確可錯不得哦,不然就找到別家去了!
我提供一個常用的方法:在代碼里你可以用循環來判斷是否為你需要的節點。
另外說一點:
XML對大小寫的要求極其嚴格。所以你的書寫不規范的話,它可是會感冒的呀!
后記:
大家熟悉動態排序和動態查詢的完成思路后會發現,其實我們的實現手法很簡單。
就是修改某一個數值,然后重新顯示。
在動態分頁的功能中我們依然是按照這個思路去完成的。