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

来源:互联网 发布:js递归遍历json树 编辑:程序博客网 时间:2024/04/28 21:42
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
五. XSL索引

如果我需要将元素的显示按一定的顺序排列,应该如何建立XSL索引呢?
我们还是来看前面的例子,还是这段代码:


<?xml version="1.0" encoding="ISO8859-1" ?>
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>
.
.
.


当XML文档被转换成HTML文件,索引应该同时建立。简单的办法就是给你的for-each元素增加一个order-by属性,就象这样:
<XSL:for-each select="CATALOG/CD" order-by=" ARTIST">
order-by属性带有一个" "或者"-" 的符号,用来定义索引的方式,是升序还是降序排列。符号后面的名字就是要索引的关键字。
例如(cd_catalog_sort.XSL):
<?xml version='1.0'?>
<XSL:stylesheet xmlns:XSL="http://www.w3.org/TR/WD-XSL">
<XSL:template match="/">
<html>
<body>
<table border="2" bgcolor="yellow">
<tr>
<th>Title</th>
<th>Artist</th>
</tr>
<XSL:for-each select="CATALOG/CD" order-by=" ARTIST">
<tr>
<td><XSL:value-of select="TITLE"/></td>
<td><XSL:value-of select="ARTIST"/></td>
</tr>
</XSL:for-each>
</table>
</body>
</html>
</XSL:template>
</XSL:stylesheet>


最后,我们用下面的HTML代码来显示索引结果,你可以自己尝试一下。
<html>
<body>
<script language="javascript">
// Load XML
var xml = new ActiveXObject("Microsoft.XMLDOM")
xml.async = false
xml.load("cd_catalog.xml")


// Load the XSL
var XSL = new ActiveXObject("Microsoft.XMLDOM")
XSL.async = false
XSL.load("cd_catalog_sort.XSL")


// Transform
document.write(xml.transformNode(XSL))
</script>


</body>
</html>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>