的choose
允许您测试多个条件,只适用于当一个(或默认情况下)相匹配。使用if
您也可以测试,但它们会被独立测试,并且每个匹配的案例都会有输出。
中添加更多细节(抱歉不得不赶去)
choose
让你测试多个的情况下,只生成的情况下输出的条件匹配的一个,或者产生一些默认的输出。例如:
<xsl:choose>
<xsl:when test='@foo=1'><!-- do something when @foo is 1--></xsl:when>
<xsl:when test='@foo=2'><!-- do something when @foo is 2--></xsl:when>
<xsl:when test='@foo=3'><!-- do something when @foo is 3--></xsl:when>
<xsl:otherwise><!-- this is the default case, when @foo is neither 1, 2 or 3--></xsl:otherwise>
</xsl:choose>
正如你所看到的“分支”的一个将采取取决于@foo
值。
随着if
,这是一个单一的测试和对测试结果产生输出:
<xsl:if test='@foo=1'><!-- do this if @foo is 1--></xsl:if>
<xsl:if test='@foo=2'><!-- do this if @foo is 2--></xsl:if>
<xsl:if test='@foo=3'><!-- do this if @foo is 3--></xsl:if>
的这里并发症是失败案例 - 当@foo
既不是1,2或3,会发生什么?这种遗漏的情况是由choose
整齐处理的 - 即具有默认动作的能力。
XSL也缺乏“其他”,你和大多数语言中找到,它允许您提供一个替代的行动应在if
测试失败 - 用单个when
一个choose
和otherwise
让你解决这个问题,但在上面我举的例子,这将是可怕的(说明为什么你不这样做..)选择
<xsl:choose>
<xsl:when test='@foo=1'><!-- do something when @foo is 1--></xsl:when>
<xsl:otherwise> <!-- else -->
<xsl:choose>
<xsl:when test='@foo=2'><!-- do something when @foo is 2--></xsl:when>
<xsl:otherwise> <!-- else -->
<xsl:choose>
<xsl:when test='@foo=2'><!-- do something when @foo is 2--></xsl:when>
<xsl:otherwise><!-- this is the case, when @foo is neither 1, 2 or 3--></xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
</xsl:otherwise>
</xsl:choose>
来源
2012-04-17 10:23:14
Nim
需要更多解释。 – 2012-04-17 10:34:35