幽默笑话
经典网文
品味人生
财富人生
休闲娱乐
非常男女
网络文摘
站长学院
火爆图铃下载
茶余饭后
>>
站长学院
>>XSL基础教程
XSL基础教程
茶余饭后
时间:2005年10月16日 来源:青苹果工作室
XSL条件选择Choose
XSL可以使用条件选择过滤XML文档。
在哪里放置选择条件
重新看看几乎在前面每个章节都看到过的XML文档:
<?xml version="1.0"?>
<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文档中增加xsl:choose、xsl:when 以及 xsl:otherwise 元素,如下:
<xsl:choose>
<xsl:when match=".[ARTIST='Bob Dylan']">
... 一些代码 ...
</xsl:when>
<xsl:otherwise>
... 一些代码 ...
</xsl:otherwise>
</xsl:choose>
现在来看看经过轻微调整的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">
<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>
在浏览器中转换
以下是在浏览器中将XML文件转换成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_choose.xsl")
// Transform
document.write(xml.transformNode(xsl))
</script>
</body>
</html>
如果使用的是Internet Explorer 5.0 或更高版本,请点击这里查看结果。
页次:
11
/11页
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
[11]
本站提供的部分资源为网上搜集,只供网友学习、交流、研究之用!
Copyright © 2005-2006 茶余饭后 All Rights Reserved.