XSL实现排序(索引)
摘自:豆豆网
完整原文链接:http://tech.ddvip.com/2006-04/11440080602917.html
如果需要将元素的显示按一定的顺序排列,应该如何建立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>
.
.
.
<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>
This posting is provided "AS IS" with no warranties, and confers no rights.
浙公网安备 33010602011771号