以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 XSL/XSLT/XSL-FO/CSS 』  (http://bbs.xml.org.cn/list.asp?boardid=8)
----  XSL 简明教程 (下)  (http://bbs.xml.org.cn/dispbbs.asp?boardid=8&rootid=&id=11299)


--  作者:anchen0617
--  发布时间:10/26/2004 10:43:00 AM

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

上面的例子改写成为:

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



--  作者:scil
--  发布时间:10/29/2004 3:41:00 PM

--  
这个例子正是用得妙啊
--  作者:scil
--  发布时间:10/29/2004 3:41:00 PM

--  
这个例子正是用得妙啊
--  作者:jimmyvk
--  发布时间:11/26/2004 12:02:00 PM

--  
写的好,顶上去
--  作者:jimmyvk
--  发布时间:11/26/2004 1:38:00 PM

--  
不过为什么我写的<xsl:when match=".[ARTIST='Bob Dylan']"> 和
< xsl:if match=".[ARTIST='Bob Dylan']"> 以及
< xsl:for -each select="CATALOG/CD" order-by="+ ARTIST">上的march属性和order-by属性不支持

--  作者:jimmyvk
--  发布时间:11/26/2004 1:45:00 PM

--  
还有“大等于”和“小等于”怎么用,举个例子瞧瞧
--  作者:jimmyvk
--  发布时间:11/29/2004 6:11:00 PM

--  
上面的问题已经知道了,但是<xsl:if>、<xsl:when>等问题还不是很清楚,麻烦知道的给个提示,谢谢!
--  作者:bluekeke
--  发布时间:10/7/2005 5:06:00 PM

--  
谢谢,好东西
W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
3,723.022ms