当前位置: 首页 > 网络学院 > XML相关教程 > XSL/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 中的 <xsl:if>


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

The <xsl:if> element is used to put a conditional test against the content of the XML file.
<xsl:if>元素的作用是:根据XML文件的内容设置一个条件语句。


The <xsl:if> Element
<xsl:if>元素

To put a conditional if test against the content of the XML file, add an <xsl:if> element to the XSL document.
如果你需要对XML文件的内容设置一个条件语句,那需要向XSL文档中添加一个<xsl:if>元素。

Syntax
语法

<xsl:if test="expression">

...
...some output if the expression is true...
...
</xsl:if>

 


Where to Put the <xsl:if> Element
<xsl:if>元素应该放在哪

To add a conditional test, add the <xsl:if> element inside the <xsl:for-each> element in the XSL file:
如果要添加一个条件语句,那么就必须在XSL文件里的<xsl:for-each>中添加一个<xsl:if>元素:

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

Note: The value of the required test attribute contains the expression to be evaluated.
注意: 所要求的(test)属性值包括了所计算的表达式的值。

The code above will only output the title and artist elements of the CDs that has a price that is higher than 10.
上述代码仅输出价格高于10的CD所对应的标题和艺术家。

The result of the transformation above will look like this:
上述代码的转换结果如下:

My CD Collection
我的CD集

Title Artist
Empire Burlesque Bob Dylan
Still got the blues Gary Moore
One night only Bee Gees
Romanza Andrea Bocelli
Black Angel Savage Rose
1999 Grammy Nominees Many

View the XML file, View the XSL file, and View the result
XML文件, XSL文件, 以及 最终结果

评论 (0) All

登陆 | 还没注册?