XML卷之實戰錦囊(2):動態查詢

來源:互聯網
上載者:User
xml|動態

動機:
查詢功能是我們在網站上見過的最普遍也是最常用的一個功能模組了。以往的資訊查詢都是串連到資料庫的,每一次點擊都必須要後台資料庫的支援。然而很多情況下使用者往往只針對某一部分的資料進行操作,這樣不但伺服器的負擔加重,而且嚴重的影響使用者瀏覽的速度。

針對這種情況我們需要將使用者需要的某一部分資料以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>Regex在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" 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對大小寫要求極其嚴格。所以你的書寫不規範的話,它可是會感冒的呀!


後記:
大家熟悉動態排序和動態查詢的完成思路後會發現,其實我們的實現手法很簡單。
就是修改某一個數值,然後重新顯示。
在動態分頁的功能中我們依然是按照這個思路去完成的。



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.