当前位置: 首页 > 网络学院 > XML相关教程 > XSL/XSLT > XSLT <xsl:if> 元素

XSL/XSLT
XSL 语言
XSLT 介绍
XSLT 浏览器
XSLT 转换
<xsl:template>
<xsl:value-of>
<xsl:for-each>
<xsl:sort>
<xsl:if>
<xsl:choose>
<xsl:apply-templates>
XSLT - 客户端
XSLT - 服务器端
XSLT - 编辑XML
XSLT 摘要
XSLT 元素参考
XSLT 函数
XSL 编辑器

XSL/XSLT 中的 XSLT <xsl:if> 元素


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-03-04   浏览: 259 ::
收藏到网摘: n/a

Definition and Usage
定义和用法

The <xsl:if> element contains a template that will be applied only if a specified condition is true.
<xsl:if>元素包含了一个模版。仅当制定的情况为true[真]时,才使用该模版。

Tip: Use <xsl:choose> in conjunction with <xsl:when> and <xsl:otherwise> to express multiple conditional tests!
提示:使用<xsl:choose>元素时,可以与<xsl:when>元素和<xsl:otherwise>元素一起使用来表达多个情况测试!


Syntax
语法

<xsl:if
test="expression">
<!-- Content: template -->
</xsl:if>

Attributes
属性

属性 描述
test expression Required. Specifies the condition to be tested
必要参数。指定需要测试的情况

Example 1
案例1

Select the values of title and artist IF the price of the CD is higher than 10:
如果CD价格高于10时,选择title值和artist值:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <table border="1"> <tr bgcolor="#9acd32"> <th>Title</th> <th>Artist</th> </tr> <xsl:for-each select="catalog/cd"> <xsl:if test="price &gt; 10"> <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>

查看XML 文件XSL 文件以及结果

Example 2
案例2

Display the title of each CD. Insert ", " between each CD-title if it is not the last CD or the last but one. If it is the last CD, add "!" behind the title. If it is the last but one CD, add ", and " behind the title:
显示每个CD值的title[标题]。如果不是最后一个CD或仅存在一个元素时,则在每个CD-title中插入“,”;如果它已是最后的CD,那么在title[标题]后面加上“,and”,具体如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/"> <html> <body> <h2>My CD Collection</h2> <p>Titles: <xsl:for-each select="catalog/cd"> <xsl:value-of select="title"/> <xsl:if test="position()!=last()"> <xsl:text>, </xsl:text> </xsl:if> <xsl:if test="position()=last()-1"> <xsl:text> and </xsl:text> </xsl:if> <xsl:if test="position()=last()"> <xsl:text>!</xsl:text> </xsl:if> </xsl:for-each> </p> </body> </html>
</xsl:template>
</xsl:stylesheet>

评论 (0) All

登陆 | 还没注册?