Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between XSLT attributes when and if

Tags:

xml

xslt

What is the difference between two given below codes? Both codes checks whether an attribute exists in a tag or not:

<xsl:choose>
  <xsl:when test="string-length(DBE:Attribute[@name='s0SelectedSite']/node()) &gt; 0"> 
    <table>
        ...
    </table>
  </xsl:when>
  <xsl:otherwise>
    <table>
        ...
    </table>
  </xsl:otherwise>
</xsl:choose>

and

<xsl:if test="@Col1-AllocValue"> 
    <xsl:copy-of select="@Col1-AllocValue"/>
</xsl:if>
like image 532
Hemant Kumar Avatar asked Apr 17 '12 10:04

Hemant Kumar


1 Answers

Structure of choose is

<xsl:choose>
    <xsl:when test="a">A</xsl:when>
    <xsl:when test="b">B</xsl:when>
    <xsl:when test="c">C</xsl:when>
    <xsl:when test="...">...</xsl:when>
    <xsl:otherwise>Z</xsl:otherwise>
</xsl:choose>

which allows multiple checks and one operation to be performed for the first test evaluating to true. xsl:otherwise is for performing default actions when none of the checks evaluates to true; in particular this facilitates an if-then-else construction (just one xsl:when alternative plus an xsl:otherwise block).

It always astounded me that xsl:if did not allow for an xsl:else alternative, but as this is available in xsl:choose construct, I guess it was judged not to add it. Maybe the next XSLT version will incorporate an xsl:else?

For the rest, the tests in xsl:when and in xsl:if do exactly the same thing: check the condition.

Note that the structure of xsl:if is simply

<xsl:if test="a">A</xsl:if>

A single

<xsl:when test="a">A</xsl:when>

would be invalid: xsl:when element is always a child of xsl:choose. And xsl:choose may have children xsl:when and xsl:otherwise only.

like image 54
Maestro13 Avatar answered Nov 10 '22 00:11

Maestro13