XSL简明教程(5)XSL的索引

五. XSL 的索引

如果我需要将元素的显示按一定的顺序排列,应该如何建立XSL的索引呢?

我们还是来看前面的例子,还是这段代码:

 1<catalog>
 2<cd>
 3<title>Empire Burlesque</title>
 4<artist>Bob Dylan</artist>
 5<country>USA</country>
 6<company>Columbia</company>
 7<price>10.90</price>
 8<year>1985</year>
 9</cd>   
10  
11.   
12  
13.   
14  
15. 
16
17  
18
19
20当XML文档被转换成HTML文件,索引应该同时建立。简单的办法就是给你的for-each元素增加一个order-by属性,就象这样:   
21  
22<xsl:for-each artist\"="" order-by='\"+' select='\"CATALOG/CD\"'>   
23  
24order-by属性带有一个\"+\"或者\"-\" 的符号,用来定义索引的方式,是升序还是降序排列。符号后面的名字就是要索引的关键字。   
25  
26例如(cd_catalog_sort.xsl):   
27  
28<?xml version=\'1.0\'?>
29<xsl:stylesheet xmlns:xsl='\"http://www.w3.org/TR/WD-xsl\"'>
30<xsl:template match='\"/\"'>
31<html>
32<body>
33<table bgcolor='\"yellow\"' border='\"2\"'>
34<tr>
35<th>Title</th>
36<th>Artist</th>
37</tr>
38<xsl:for-each artist\"="" order-by='\"+' select='\"CATALOG/CD\"'>
39<tr>
40<td><xsl:value-of select='\"TITLE\"/'></xsl:value-of></td>
41<td><xsl:value-of select='\"ARTIST\"/'></xsl:value-of></td>
42</tr>
43</xsl:for-each>
44</table>
45</body>
46</html>
47</xsl:template>
48</xsl:stylesheet>
49
50  
51
52
53最后,我们用下面的HTML代码来显示索引结果,你可以自己尝试一下。   
54  
55<html>
56<body>
57<script language='\"javascript\"'>   
58  
59// Load XML   
60  
61var xml = new ActiveXObject(\"Microsoft.XMLDOM\")   
62  
63xml.async = false   
64  
65xml.load(\"cd_catalog.xml\") 
66
67  
68
69
70// Load the XSL   
71  
72var xsl = new ActiveXObject(\"Microsoft.XMLDOM\")   
73  
74xsl.async = false   
75  
76xsl.load(\"cd_catalog_sort.xsl\") 
77
78  
79
80
81// Transform   
82  
83document.write(xml.transformNode(xsl))   
84  
85</script>
86</body>
87</html></xsl:for-each></catalog>
Published At
Categories with Web编程
Tagged with
comments powered by Disqus