- 代碼:
-
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>年末經(jīng)典分舵聚會完全手冊 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錯誤信息總匯</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卷之實戰(zhàn)錦囊(1):動態(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 taxis(x) { stylesheet=document.XSLDocument; source=document.XMLDocument; sortField=document.XSLDocument.selectSingleNode("http://@order-by"); sortField.value=x; Layer1.innerHTML=source.documentElement.transformNode(stylesheet); } </script> </head> <body> <p align="center"><span>XML卷之實戰(zhàn)錦囊(1):動態(tài)排序</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')">發(fā)表時間</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 是數(shù)據(jù)文件,相信大家都不會有問題。 2)paixu.xsl 是格式文件,有幾個地方要注意。 |
(1)腳本中:
sortField=document.XSLDocument.selectSingleNode("http://@order-by"); 作用是:找到有屬性為order-by的第一個節(jié)點,因此它對應的節(jié)點就是 <xsl:apply-templates select="team" order-by="blue_ID"/> 因此在初次onLoad的時候order-by的value值是blue_ID。 而我們就是通過重新定義order-by的value值來達到排序的目的。 Layer1.innerHTML=source.documentElement.transformNode(stylesheet); 作用是:轉(zhuǎn)化XML數(shù)據(jù)后更改Layer1,因此在傳出參數(shù)'blue_name'后, <td style="cursor:s-resize" onClick="taxis('blue_name)">姓名</td> 我們將order-by的value值修改為是'blue_name',即以'blue_name'為排序方式。 繼而通過重新顯示Layer1的innerHTML值來顯示新的排序內(nèi)容。
(2)文本中:
order-by 這個可不能少哦,不然就找不到了,效果嘛,你瞧瞧看吧!!
<?xml version="1.0" encoding="gb2312" ?> 另外說一點: 在大多的XML教科書中所顯示的代碼中很少會加上encoding="gb2312" , 因此我們在XML中用到中文的時候會報錯,原因就是沒有寫這個申明。
|
- 后記:
- 大家熟悉動態(tài)排序完成思路后會發(fā)現(xiàn),其實我們的實現(xiàn)手法很簡單。
就是修改order-by的數(shù)值,然后重新顯示。
|