IT学习网(ITxuexi.com) - 打造中国最好的IT学习平台!

IT学习网

<xsl:if>

时间:2009-03-01 23:46来源: 作者:admin 点击:

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文件, 以及 最终结果

本文来源于:IT学习网 http://www.itxuexi.com/learn/5303.html

顶一下
(0)
0%
踩一下
(0)
0%
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
用户名: 验证码:点击我更换图片