を使用chooseすると、複数の条件をテストして、1 つが一致した場合 (またはデフォルトのケース) にのみ適用できます。あなたifもテストできますが、それらは個別にテストされ、一致するケースごとに出力があります。
詳細を追加します(急いで申し訳ありません)
choose複数のケースをテストし、条件の 1 つが一致する場合にのみ出力を生成したり、デフォルトの出力を生成したりできます。例えば:
<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>
ご覧のとおり、 の値に応じて「分岐」の1 つが@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>
ここで複雑なのは、失敗の場合です。1、2、3 のいずれでもない場合はどうなる@fooでしょうか。この欠落しているケースは、 によって適切に処理されます。つまり、デフォルトアクションchooseを持つ機能です。
XSL には、他のほとんどの言語に見られる "else" もありません。これにより、ifテストが失敗しchooseた場合に代替アクションwhenを提供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>