xsl:if
xsl:if类似普通程序语言的if条件语句,允许设定节点满足某个条件时,被模板处理。xsl:if的语法格式为:
<xsl:if test=布尔表达式>
template body
</xsl:if>
template body
</xsl:if>
例如:
<xsl:if test="//**Response/out/annualCompensation/ishift > 0">
<strong>Shift Differential</strong><br />
The amount shown on this statement reflects any shift differential you have received in the past 52 pay periods.
<br/><br/><br/>
</xsl:if>
<strong>Shift Differential</strong><br />
The amount shown on this statement reflects any shift differential you have received in the past 52 pay periods.
<br/><br/><br/>
</xsl:if>
--------------------------------------------------------------------------------------------------------------------------------------------------------
<xsl:template match="PEOPLE">
<xsl:if test="@name">
<p><xsl:value-of select="@name"/></p>
</xsl:if>
</xsl:template>
<xsl:if test="@name">
<p><xsl:value-of select="@name"/></p>
</xsl:if>
</xsl:template>
这段代码的意思是检测PEOPLE节点下所有的元素,如果发现有<name>元素,则将<name>元素的值输出。其中@符号是统配符,表示节点下所有的元素。
==================================================================================================================================
xsl:choose, xsl:when 和 xsl:otherwise
xsl:if语法没有else的属性。如果我们要进行多项选择,那么就要使用xsl:choose / xsl:when / xsl:otherwise系列流程控制语法了。
具体的使用请看下面的XSL文件例子:
<xsl:template match="PEOPLE">
<xsl:choose>
<xsl:when test="@name = 'keyw'">
<B><xsl:value-of select="@name"/></B>
</xsl:when>
<xsl:when test="@name">
<I><xsl:value-of select="@name"/></I> <!--
@符号是统配符,表示节点下所有的元素。-->
</xsl:when>
<xsl:otherwise>
No name available
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:choose>
<xsl:when test="@name = 'keyw'">
<B><xsl:value-of select="@name"/></B>
</xsl:when>
<xsl:when test="@name">
<I><xsl:value-of select="@name"/></I> <!--
@符号是统配符,表示节点下所有的元素。-->
</xsl:when>
<xsl:otherwise>
No name available
</xsl:otherwise>
</xsl:choose>
</xsl:template>
说明:首先在PEOPLE节点下寻找<name>属性值为keyw的元素,如果找到,将keyw用粗体输出;
如果没有发现值为keyw
的<name>元素,则将所有的<name>元素的值都用斜体输出;如果没有发现任何<name>元素,则显示
"No name available"。