Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

when to use xsl:if and when to use xsl:choose/xsl:when in XSLT

Tags:

xslt

I know this may seem like a dumb/newbie question, but I'm fairly new to XSLT (though I'm starting to come around to it and see it's capabilities).

When is it appropriate to use xsl:if and when is it appropriate to use xsl:choose/xsl:when?

I have been using choose/when/otherwise when I want to have an "else" option. Is this correct?

For instance I have some places where I'm doing:

<xsl:choose>
  <xsl:when test="count(entry) != 0">
    put positive outcome here
  </xsl:when>
  <xsl:otherwise>
    put something else here
  </xsl:otherwise>
</xsl:choose>

would xsl:if be better?

Thanks for the input.

like image 736
jjasper0729 Avatar asked Dec 14 '25 12:12

jjasper0729


1 Answers

Use xsl:if for simple cases where you just want to test if an expression is true. (Note that there is no corresponding xsl:else.)

<xsl:if test="expression">
    output if the expression is true
</xsl:if>

Use xsl:choose for cases where you have some alternate output when the expressions is false.

<xsl:choose>
    <xsl:when test="expression">
        output if the expression is true
    </xsl:when>
    <xsl:otherwise>
        output if the expression is false
    </xsl:otherwise>
</xsl:choose>
like image 116
Bill the Lizard Avatar answered Dec 17 '25 08:12

Bill the Lizard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!