首頁  >  文章  >  後端開發  >  XML實戰秘籍第二卷:動態查詢

XML實戰秘籍第二卷:動態查詢

巴扎黑
巴扎黑原創
2017-03-19 15:43:181414瀏覽

[導讀] 查詢功能是我們在網站上見過的最普遍也是最常用的一個功能模組了。以往的資訊查詢都是連接到資料庫的,每一次點擊都必須要後台資料庫的支援。然而許多情況下使用者往往只針對某一部分的資料進行操作,這樣不但服務

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

針對這種情況我們需要將使用者需要的某一部分資料以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=&#39;"+x+"&#39;]";
Layer1.innerHTML=source.documentElement.transformNode(stylesheet);
}
else {alert("请输入筛选条件!");} 
}
</script>
</head>
<body>
<p align="center"><span>XML卷之实战锦囊(2):动态查询</span></p>
<p id="Layer1" name="Layer1">
<xsl:apply-templates select="BlueIdea" />
</p>
<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]就是找到的第二個節點,它對應的節點是
9b92dc4fa42eb944186860fecb66172b

參數x 是文字方塊中輸入的數值。
我們將select="team" 的搜尋條件修改為select="team[blue_ID='x']"
作用是:增加判斷條件,只有blue_ID的數值等於 x 的XML資料才顯示出來。
當然大家可以豐富判斷的條件,我在這裡做的簡單判斷是為了讓大家更容易理解。
最後透過重新顯示Layer1的innerHTML值來顯示新的排序內容。



(2)文字中:

select="team"
在我這裡它是sortField[1],但你在做的時候可能就會更改。
那麼你就一定要計算準確可錯不得哦,不然就找到別家去了!
我提供一個常用的方法:在程式碼裡你可以用迴圈來判斷是否為你需要的節點。


另外說一點:
XML對大小寫的要求極為嚴格。所以你的書寫不規範的話,它可是會感冒的呀!                        

以上是XML實戰秘籍第二卷:動態查詢的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn