motivation:
The sorting function makes the data on our page more user-friendly, and is a very common functional effect we have seen on the website. In the past, automatic sorting was done with a large amount of script code, which is a difficult thing for ordinary enthusiasts. However, it is much easier to process using XML. Make your page more gorgeous, haha, are you moved too!
Material:
Dynamic sorting of XML volumes
There are 2 files: and
effect:
Without refreshing the page, the data is reordered and displayed according to the user's own needs, effectively improving the data interaction function and making your page more colorful.
Effect:
Browse here
Code:
<?xml version="1.0" encoding="gb2312" ?>
<?xml-stylesheet type="text/xsl" href="" ?>
<BlueIdea>
<team>
<blue_ID>1</blue_ID>
<blue_name>Sailflying</blue_name>
<blue_text>A simple sort</blue_text>
<blue_time>2002-1-11 17:35:33</blue_time>
<blue_class>XML topic</blue_class>
</team>
<team>
<blue_ID>2</blue_ID>
<blue_name>flyingbird</blue_name>
<blue_text>Marrying you is what you want to hurt</blue_text>
<blue_time>2001-09-06 12:45:51</blue_time>
<blue_class>Flooding Essence</blue_class>
</team>
<team>
<blue_ID>3</blue_ID>
<blue_name>Kaozi</blue_name>
<blue_text>Application of regular expressions in UBB forum</blue_text>
<blue_time>2001-11-23 21:02:16</blue_time>
<blue_class>Web Programming Essence</blue_class>
</team>
<team>
<blue_ID>4</blue_ID>
<blue_name>Taiyilang</blue_name>
<blue_text>Complete manual of classic branch party at the end of the year v0.1</blue_text>
<blue_time>2000-12-08 10:22:48</blue_time>
<blue_class>Forum irrigation area</blue_class>
</team>
<team>
<blue_ID>5</blue_ID>
<blue_name>mmkk</blue_name>
<blue_text>Asp error message summary</blue_text>
<blue_time>2001-10-13 16:39:05</blue_time>
<blue_class>javascript script</blue_class>
</team>
</BlueIdea>
<?xml version="1.0" encoding="gb2312" ?>
<xsl:stylesheet xmlns:xsl="http:///TR/WD-xsl">
<xsl:template match="/">
<html>
<head>
<title> XML volume practical tips (1): Dynamic sorting</title>
<style>
body,BlueIdea,team,blue_ID,blue_name,blue_text,blue_time,blue_class{ font: 12px "Stick", "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=;
source=;
sortField=("//@order-by");
=x;
=(stylesheet);
}
</script>
</head>
<body>
<p align="center"><span>XML volume practical tips (1): Dynamic sorting</span></p>
<div 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')">Number</td>
<td style="cursor:s-resize" onClick="taxis('blue_name')">Name</td>
<td style="cursor:s-resize" onClick="taxis('blue_text')">Topic</td>
<td style="cursor:s-resize" onClick="taxis('blue_time')">Published time</td>
<td style="cursor:s-resize" onClick="taxis('blue_class')">Classification</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>
explain:
1) It is a data file, I believe everyone will not have any problems.
2) It is a format file, there are several things to pay attention to.
(1) In the script:
sortField=("//@order-by");
The function is: find the first node with the attribute order-by, so its corresponding node is
<xsl:apply-templates select="team" order-by="blue_ID"/>
Therefore, the value of order-by is blue_ID when the first onLoad is blue_ID.
And we achieve the purpose of sorting by redefining the value of order-by.
=(stylesheet);
The function is: after converting XML data, change Layer1, so after passing out the parameter 'blue_name',
<td style="cursor:s-resize" onClick="taxis('blue_name)">Name</td>
We modify the value of order-by to be 'blue_name', that is, the order is 'blue_name'.
Then, the new sorted content is displayed by redisplaying the innerHTML value of Layer1.
(2) In the text:
order-by
This one cannot be missed, otherwise you won’t be able to find it. As for the effect, you can take a look! !
<?xml version="1.0" encoding="gb2312" ?>
Another point:
In most XML textbooks, encoding="gb2312" is rarely added to the code shown in the code.
Therefore, when we use Chinese in XML, we will report an error because we did not write this statement.
postscript:
When you are familiar with the idea of dynamic sorting, you will find that our implementation method is actually very simple.
It is to modify the order-by value and then display it again.
We still complete the functions of dynamic query and dynamic paging according to this idea.