405人参与 • 2024-05-19 • Xml
xsl中的if,首先,介绍xsl元素<xsl:if>的语法结构:
语法:
<xsl:if expr="script-expression" language="language-name" test="pattern">
属性:
expr ── 脚本语言表达式,计算结果为"真"或"假";如果结果为"真",且通过test,则在输出中显示其中内容(可省略此项属性)。
language ── expr属性中表达式的脚本语言类型,其取值与html标记script的language属性的取值相同,缺省为"jscript"。
test ──源数据测试条件。
示例:
此处以一份报表为例,文件名为report.xml,其内容如下:
<?xml version="1.0" encoding="gb2312"?>
<?xml-stylesheet type="text/xsl" href="report.xsl"?>
<document>
<report>
<class>
甲班
</class>
<q1>50</q1>
<q2>70</q2>
<q3>30</q3>
<q4>10</q4>
</report>
<report>
<class>
乙班
</class>
<q1>20</q1>
<q2>30</q2>
<q3>40</q3>
<q4>50</q4>
</report>
<report>
<class>
丙班
</class>
<q1>70</q1>
<q2>40</q2>
<q3>20</q3>
<q4>10</q4>
</report>
</document>
我们采用xsl模板结合今天所学的<xsl:if>,为其编写一个xsl文档,要求季度产量小于等于20的用红色表示,文件名为report.xsl,内容如下:
<?xml version="1.0" encoding="gb2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/tr/wd-xsl">
<xsl:template match="/">
<html><head><title>1999年生产统计</title></head>
<body><xsl:apply-templates select="document"/></body>
</html>
</xsl:template>
<xsl:template match="document">
<h3>1999年生产统计</h3>
<table border="1" cellspacing="0">
<th>班组</th>
<th>一季度</th>
<th>二季度</th>
<th>三季度</th>
<th>四季度</th>
<xsl:apply-templates select="report"/>
</table>
</xsl:template>
<xsl:template match="report">
<tr>
<td><xsl:value-of select="class"/></td>
<td><xsl:apply-templates select="q1"/></td>
<td><xsl:apply-templates select="q2"/></td>
<td><xsl:apply-templates select="q3"/></td>
<td><xsl:apply-templates select="q4"/></td>
</tr>
</xsl:template>
<xsl:template match="q1|q2|q3|q4">
<!--此处测试产量,如小于等于20则添加一style属性color,其值为red(红色)-->
<xsl:if test=".[value()$le$20]">
<xsl:attribute name="style">color:red</xsl:attribute>
</xsl:if>
<xsl:value-of/>
</xsl:template>
</xsl:stylesheet>
说明:
q1|q2|q3|q4 ── 标记q1、q2、q3、q3均用此模板确定输出
$le$ ── 是关系运算符中的"小于等于",其它关系有小于($lt$)、大于($gt$)、大于等于($ge$)、等于($eq$)、不等于($ne$)等。
. ── 表示引用当前标记。
[ ] ── 表示筛选,只有满足筛选条件的标记才能被选取。
value() ──xsl函数,其他常用xsl函数有text()、end()、index()等。
下期,我们将学习xsl的另外三个元素,可对同一数据进行多次测试,根据不同条件产生相应输出。
您想发表意见!!点此发布评论
版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。
发表评论