DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> XML卷之實戰錦囊(1):動態排序 - asp.net
XML卷之實戰錦囊(1):動態排序 - asp.net
編輯:XML詳解     
r>排序功能讓我們頁面上的數據顯的更人性化,是我們在網站上見過的很普遍的一個功能效果了。以往的自動排序都是用大量的腳本代碼來完成的,對一般的愛好者來說這是件困難的事情。然而用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
程序代碼: [ 復制代碼到剪貼板 ]
<?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>
<div id="Layer1" name="Layer1">
<xsl:apply-templates select="BlueIdea" />
</div>
</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(@#blue_ID@#)">編號</td>
<td style="cursor:s-resize" onClick="taxis(@#blue_name@#)">姓名</td>
<td style="cursor:s-resize" onClick="taxis(@#blue_text@#)">主題</td>
<td style="cursor:s-resize" onClick="taxis(@#blue_time@#)">發表時間</td>
<td style="cursor:s-resize" onClick="taxis(@#blue_class@#)">歸類</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)paixu.XML 是數據文件,相信大家都不會有問題。
2)paixu.xsl 是格式文件,有幾個地方要注意。
(1)腳本中:

sortFIEld=document.XSLDocument.selectSingleNode("//@order-by");
作用是:找到有屬性為order-by的第一個節點,因此它對應的節點就是
<xsl:apply-templates select="team" order-by="blue_ID"/>
因此在初次onLoad的時候order-by的value值是blue_ID。
而我們就是通過重新定義order-by的value值來達到排序的目的。


Layer1.innerHtml=source.documentElement.transformNode(stylesheet);
作用是:轉化XML數據後更改Layer1,因此在傳出參數@#blue_name@#後,
<td style="cursor:s-resize" onClick="taxis(@#blue_name)">姓名</td>
我們將order-by的value值修改為是@#blue_name@#,即以@#blue_name@#為排序方式。
繼而通過重新顯示Layer1的innerHtml值來顯示新的排序內容。

(2)文本中:

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

程序代碼: [ 復制代碼到剪貼板 ]
<?XML version="1.0" encoding="gb2312" ?>

另外說一點:
在大多的XML教科書中所顯示的代碼中很少會加上encoding="gb2312" ,
因此我們在XML中用到中文的時候會報錯,原因就是沒有寫這個申明。


後記:
大家熟悉動態排序完成思路後會發現,其實我們的實現手法很簡單。
就是修改order-by的數值,然後重新顯示。
在動態查詢和動態分頁的功能中我們依然是按照這個思路去完成的。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved