首頁  >  文章  >  後端開發  >  XML卷之實戰錦囊(1):動態排序

XML卷之實戰錦囊(1):動態排序

黄舟
黄舟原創
2017-02-10 16:09:321782瀏覽

動機:
排序功能讓我們頁面上的資料顯的更人性化,是我們在網站上見過的很普遍的一個功能效果了。以往的自動排序都是用大量的腳本程式碼來完成的,對一般的愛好者來說這是件困難的事。然而用XML來處理的話就簡單多了。讓自己的頁面更加絢麗,哈哈,您是不是也心動了呢!

材料:
XML卷之動態排序
有2個檔案:paixu.xml 和paixu.xsl

作用:
在不刷新頁面的情況下更據用戶自己的需要對資料重新進行排序顯示,有效的提升數據互動功能,讓自己的頁面更加絢麗多彩。 
效果:
瀏覽這裡 
程式碼:
paixu.xml

<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="paixu.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>

 

paixu.xsl

2)paixu.xsl 是格式文件,有幾個地方要注意。

(1)腳本中:

sortField=document.XSLDocument.selectSingleNode("//@order-by");
作用是:找到有屬性為order-by的第一個節點,因此它對應的節點就是

因此在初次onLoad的時候order-by的value值是blue_ID。

而我們就是透過重新定義order-by的value值來達到排序的目的。

<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title> XML卷之实战锦囊(1):动态排序</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 taxis(x) 
{ 
stylesheet=document.XSLDocument; 
source=document.XMLDocument; 
sortField=document.XSLDocument.selectSingleNode("//@order-by");
sortField.value=x; 
Layer1.innerHTML=source.documentElement.transformNode(stylesheet); 
}
</script>
</head>
<body>
<p align="center"><span>XML卷之实战锦囊(1):动态排序</span></p>
<p id="Layer1" name="Layer1">
<xsl:apply-templates select="BlueIdea" />
</p>
</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 style="cursor:s-resize" onClick="taxis(&#39;blue_ID&#39;)">编号</td>
<td style="cursor:s-resize" onClick="taxis(&#39;blue_name&#39;)">姓名</td>
<td style="cursor:s-resize" onClick="taxis(&#39;blue_text&#39;)">主题</td>
<td style="cursor:s-resize" onClick="taxis(&#39;blue_time&#39;)">发表时间</td>
<td style="cursor:s-resize" onClick="taxis(&#39;blue_class&#39;)">归类</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>


作用是:轉換XML資料後更改Layer1,因此在傳出參數'blue_name'後,

Layer1.innerHTML=source.documentElement.transformNode(stylesheet);


我們將order-by的value值修改為是'blue_name',即以'blue_name'為排序方式。
繼而透過重新顯示Layer1的innerHTML值來顯示新的排序內容。

(2)文字中:

order-by

這個可不能少哦,不然就找不到了,效果嘛,你瞧瞧看吧! !

<td style="cursor:s-resize" onClick="taxis(&#39;blue_name)">姓名</td>

在大多的XML教科書中所顯示的程式碼中很少會加上encoding="gb2312" ,

因此我們在XML中用到中文的時候會報錯,原因就是沒有寫這個申明。

 

後記:

大家熟悉動態排序完成思維後會發現,其實我們的實作手法很簡單。

就是修改order-by的數值,然後重新顯示。

在動態查詢和動態分頁的功能中我們依然是按照這個思路去完成的。 



以上就是XML卷之實戰錦囊(1):動態排序的內容,更多相關內容請關注PHP中文網(www.php.cn)!

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