動(dòng)機(jī):
查詢功能是我們?cè)诰W(wǎng)站上見過的最普遍也是最常用的一個(gè)功能模塊了。以往的信息查詢都是連接到數(shù)據(jù)庫的,每一次點(diǎn)擊都必須要后臺(tái)數(shù)據(jù)庫的支持。然而很多情況下用戶往往只針對(duì)某一部分的數(shù)據(jù)進(jìn)行操作,這樣不但服務(wù)器的負(fù)擔(dān)加重,而且嚴(yán)重的影響用戶瀏覽的速度。
針對(duì)這種情況我們需要將用戶需要的某一部分?jǐn)?shù)據(jù)以XML的方式傳遞到客戶端,用戶對(duì)這些數(shù)據(jù)可以很方便的進(jìn)行操作。既方便了用戶,又減輕了服務(wù)器數(shù)據(jù)庫的負(fù)擔(dān)。何樂而不為呢!而且這項(xiàng)功能可以通用到其他眾多模塊,因此添加了這個(gè)動(dòng)態(tài)查詢功能。
材料:
XML卷之動(dòng)態(tài)查詢
有2個(gè)文件:search.xml 和 search.xsl
作用:
在不刷新頁面的情況下對(duì)數(shù)據(jù)進(jìn)行過濾篩選,有效的提高數(shù)據(jù)查詢的功能。
效果:
瀏覽這里
代碼:
search.xml
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="http://www.companysz.com/search.xsl" ?>
<BlueIdea>
<team>
<blue_ID>1</blue_ID>
<blue_name>Sailflying</blue_name>
<blue_text>一個(gè)簡(jiǎn)單的查詢</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>正則表達(dá)式在UBB論壇中的應(yīng)用</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>年末經(jīng)典分舵聚會(huì)完全手冊(cè) v0.1</blue_text>
<blue_time>2000-12-08 10:22:48</blue_time>
<blue_class>論壇灌水區(qū)</blue_class>
</team>
<team>
<blue_ID>5</blue_ID>
<blue_name>mmkk</blue_name>
<blue_text>Asp錯(cuò)誤信息總匯</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卷之實(shí)戰(zhàn)錦囊(2):動(dòng)態(tài)查詢</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("請(qǐng)輸入篩選條件!");}
}
</script>
</head>
<body>
<p align="center"><span>XML卷之實(shí)戰(zhàn)錦囊(2):動(dòng)態(tài)查詢</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 >請(qǐng)輸入篩選條件 : </span>
blue_ID= <input type="text" name="searchtext" size="1" maxlength="1" />
<input type="button" class="button" onClick="searchtext(document.all.searchtext.value)" value="http://www.companysz.com/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>編號(hào)</td>
<td>姓名</td>
<td>主題</td>
<td>發(fā)表時(shí)間</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 是數(shù)據(jù)文件,相信大家都不會(huì)有問題。
2)search.xsl 是格式文件,有幾個(gè)地方要注意。
(1)腳本中:
sortField=document.XSLDocument.selectNodes("//@select");
作用是:找到所有屬性為select的節(jié)點(diǎn)。這個(gè)和我在動(dòng)態(tài)排序中說到的
sortField=document.XSLDocument.selectSingleNode("//@order-by");
有些不一樣了。大家注意這個(gè)小小的區(qū)別以及各自的功能。
sortField[1].value="team[blue_ID='" x "']";
因此sortField[1]就是找到的第二個(gè)節(jié)點(diǎn),它對(duì)應(yīng)的節(jié)點(diǎn)就是
<xsl:apply-templates select="team" order-by="blue_ID"/>
參數(shù) x 是文本框中輸入的數(shù)值。
我們將select="team" 的搜索條件修改為select="team[blue_ID='x']"
作用是:增加判斷條件,只有blue_ID的數(shù)值等于 x 的XML數(shù)據(jù)才顯示出來。
當(dāng)然大家可以豐富判斷的條件,我在這里做的簡(jiǎn)單判斷是為了讓大家更容易理解。
最后通過重新顯示Layer1的innerHTML值來顯示新的排序內(nèi)容。
(2)文本中:
select="team"
在我這里它是 sortField[1],但你在做的時(shí)候可能就會(huì)更改。
那么你就一定要計(jì)算準(zhǔn)確可錯(cuò)不得哦,不然就找到別家去了!
我提供一個(gè)常用的方法:在代碼里你可以用循環(huán)來判斷是否為你需要的節(jié)點(diǎn)。
另外說一點(diǎn):
XML對(duì)大小寫的要求極其嚴(yán)格。所以你的書寫不規(guī)范的話,它可是會(huì)感冒的呀!
后記:
大家熟悉動(dòng)態(tài)排序和動(dòng)態(tài)查詢的完成思路后會(huì)發(fā)現(xiàn),其實(shí)我們的實(shí)現(xiàn)手法很簡(jiǎn)單。
就是修改某一個(gè)數(shù)值,然后重新顯示。
在動(dòng)態(tài)分頁的功能中我們依然是按照這個(gè)思路去完成的。
新聞熱點(diǎn)
疑難解答
圖片精選