当前位置: 移动技术网 > IT编程>网页制作>XML > XSL简明教程(5)XSL的索引

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

2017年12月08日  | 移动技术网IT编程  | 我要评论
原著:jan egil refsnes 翻译:阿捷

五. 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>

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网