XML卷之实战锦囊(1):动态排序
来源:互联网 发布时间:2008-09-05 17:20:00
动机:
排序功能让我们页面上的数据显的更人性化,是我们在网站上见过的很普遍的一个功能效果了。以往的自动排序都是用大量的脚本代码来完成的,对一般的爱好者来说这是件困难的事情。然而用XML来处理的话就简单多了。让自己的页面更加绚丽,哈哈,您是不是也心动了呢!
材料:
XML卷之动态排序
有2个文件:paixu.xml 和 paixu.xsl
作用:
在不刷新页面的情况下更据用户自己的需要对数据重新进行排序显示,有效的提高数据互动功能,让自己的页面更加绚丽多彩。
效果:
浏览这里
代码:
paixu.xml
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="http://www.knowsky.com/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的数值,然后重新显示。
在动态查询和动态分页的功能中我们依然是按照这个思路去完成的。
猜你喜欢
- 之前对bottle做过不少的介绍,也写过一些文章来说明bottle的缺点,最近发现其实之前有些地方说的不太公平,所以趁此机会也来更正一下。&
- 在网络浏览器软件中,可以Internet Explorer (IE)现在是一种标准的软件。可以看到,运行不同版本的Windows操作系统(和
- 动态链接库在Windows中为.dll文件,在linux中为.so文件。以linux平台为例说明python调用.so文件的使用方法。本例中
- 简单介绍:Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,Selenium 可以直接运行在浏览器上,它支持所有
- Agent 服务启动后又停止本地计算机上的SQL Server Agent 服务启动后又停止了,一些服务自动停止,如果它们没有什么可做的例如
- 本文实例为大家分享了python实现图书管理系统的具体代码,供大家参考,具体内容如下添加新书查询借阅二次添加新书(读取已有的.xls并修改)
- 一、旧式的字符串格式化% 操作符参考以下示例:>>> name = "Eric">>>
- 本文研究的主要是python+matplotlib实现动态绘制图片(交互式绘图)的相关内容,具体介绍和实现代码如下所示。最近在研究动态障碍物
- 乍一看到列表推导式你可能会感到疑惑。它们是一种创建和使用列表的简洁方式。理解列表推导式是有用的,因为你可能在其他人的代码里看到列表推导式。下
- MySQL中concat函数 使用方法:CONCAT(str1,str2,…)返回结果为连接参数产生的字符串。如有任何一个参数为NULL ,
- 目录Blazor Server 应用程序中进行 HTTP 请求一、第三方 Web API 概览二、从 Blazor Sever 应用程序开始
- 本文实例讲述了python计算书页码的统计数字问题,是Python程序设计中一个比较典型的应用实例。分享给大家供大家参考。具体如下:问题描述
- 这篇文章主要介绍了Python globals()和locals()对比详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的
- 如题,首先当然是要打开京东的手机页面因为要获取不同页面的所有手机图片,所以我们要跳转到不同页面观察页面地址的规律,这里观察第二页页面由观察可
- 因为工作中需要,需要生成一个带表格的图片例如:直接在html中写一个table标签,然后单独把表格部分保存成图片或者是直接将excel中的内
- set转成list方法如下: list转成set方法如下:s = set('12342212') &n
- async / await是ES7的重要特性之一,也是目前社区里公认的优秀异步解决方案。目前,async / await这个特性已经是sta
- 本文研究的主要是Python生成器及其应用,具体如下。一、定义可以理解为一种数据类型,这种数据类型自动实现了迭代器协议(其他的数据类型需要调
- 前言本文主要给大家介绍关于python中__init__、__new__和__call__方法的相关内容,分享出来供大家参考学习,下面话不多
- 你用过css么?当然,我是指你喜欢做网页的话,用过?很好,那你用过它的特效么?没有?那请跟我来。让我先