XSL 简明教程 (下)

XSL 简明教程 (下)

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

 
一 .XSL 入 门 

二 .XSL的转换 

三 .XSL--在客户端的实现

四 .XSL --- 在服务器端的实现 


五 . XSL 的索引 

六 . XSL的过滤和查询

七 . XSL 的控制语句

  
 
六 . XSL的过滤和查询 章 节 导 航 
如果我们希望只显示满足一定的条件的 XML数据应该怎么做呢?还是上面的例子代码,我们只需要在 xsl:for -each元素的select属性中加入参数就可以,类似: 

< xsl:for -each select="CATALOG/CD[ARTIST='Bob Dylan']"> 

参数的逻辑选择有: 

= (等于) 

=! (不等于) 

&LT& 小于 

&GT& 大于等于 

和前面同样的例子 ( 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[ARTIST='Bob Dylan']"> 

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

你可以自己测试一下,看到的结果有什么不同。 

 

一 .XSL 入 门 

二 .XSL的转换 

三 .XSL--在客户端的实现

四 .XSL --- 在服务器端的实现 


五 . XSL 的索引 

六 . XSL的过滤和查询

七 . XSL 的控制语句
 
七 . XSL 的控制语句  章 节 导 航 
1.条件语句if...then 

XSL同样还有条件语句(呵呵~~好厉害吧, 象 程序语言一样)。具体的语法是增加一个 xsl:if 元素,类似这样 

< xsl: if match=".[ARTIST='Bob Dylan']"> 

... some output ... 

</ xsl:if > 

注意:

1.我见过的格式是:< xsl: if test=".[ARTIST='Bob Dylan']"> ,而不是< xsl: if match=".[ARTIST='Bob Dylan']"> 

2.在""中有一个".",不应漏掉.

上面的例子改写成为: 

<?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"> 

< xsl:if match=".[ARTIST='Bob Dylan']"> 

< tr > 

<td>< xsl:value -of select="TITLE"/></td> 

<td>< xsl:value -of select="ARTIST"/></td> 

</ tr > 

</ xsl:if > 

</ xsl:for -each> 

</table> 

</body> 

</html> 

</ xsl:template > 

</ xsl:stylesheet > 

2. XSL 的Choose 

choose的用途是出现多个条件,给出不同显示结果。具体的语法是增加一组 xsl:choose,xsl:when,xsl:otherwise 元素: 

< xsl :choose > 

< xsl:when match=".[ARTIST='Bob Dylan']"> 

... some code ... 

</ xsl:when > 

< xsl:otherwise > 

... some code .... 

</ xsl :otherwise > 

</ xsl:choose > 

上面的例子改写成为: 

<?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"> 

< tr > 

<td>< xsl:value -of select="TITLE"/></td> 

< xsl:choose > 

< xsl:when match=".[ARTIST='Bob Dylan']"> 

<td bgcolor ="#ff0000">< xsl:value -of select="ARTIST"/></td> 

</ xsl:when > 

< xsl:otherwise > 

<td>< xsl:value -of select="ARTIST"/></td> 

</ xsl:otherwise > 

</ xsl:choose > 

</ tr > 

</ xsl:for -each> 

</table> 

</body> 

</html> 

</ xsl:template > 

</ xsl:stylesheet >
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值