XSLT: Multiple conditional variable assignment

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • obanite
    New Member
    • Oct 2007
    • 5

    XSLT: Multiple conditional variable assignment

    Hello,

    Is there a better way of doing:

    Code:
      <xsl:variable name="frlink">
      <xsl:choose>
      <xsl:when test="@isFriend = '0'"><xsl:value-of select="concat('friends.php?add=', @uid)"/></xsl:when>
      <xsl:otherwise> <xsl:value-of select="concat('friends.php?add=', @uid)"/></xsl:otherwise>
      </xsl:choose>
      </xsl:variable>
      <xsl:variable name="frstr">
      <xsl:choose>
      <xsl:when test="@isFriend = '0'"><xsl:value-of select="'FR_ADD_TO_FRIENDS'"/></xsl:when>
      <xsl:otherwise> <xsl:value-of select="'FR_REMOVE_FRIEND'"/></xsl:otherwise>
      </xsl:choose>
      </xsl:variable>
    I.e. in an procedural language I would do:

    if(a)
    {
    i = someval; j = someval; k = someval;
    }
    else
    {
    i = someotherval; j = someotherval; k = someotherval;
    }

    In XSLT it seems to be a case of put the conditional inside every single variable assignment - is there a better way?

    Thanks,

    Dave
  • jkmyoung
    Recognized Expert Top Contributor
    • Mar 2006
    • 2057

    #2
    There is another way, although this has it's own problems.

    You could create a super variable


    [code=xml]
    <xsl:variable name="super">
    <super>
    <xsl:choose>
    <xsl:when test="@isFriend = '0'">
    <frlink><xsl:va lue-of select="concat( 'friends.php?ad d=', @uid)"/></frlink>
    <frstr><xsl:val ue-of select="'FR_ADD _TO_FRIENDS'"/></frstr>
    </xsl:when>
    <xsl:otherwis e>
    <frlink><xsl:va lue-of select="concat( 'friends.php?ad d=', @uid)"/></frlink>
    <frstr><xsl:val ue-of select="'FR_REM OVE_FRIEND'"/></frstr>
    </xsl:otherwise>
    </xsl:choose>
    </super>
    </xsl:variable>
    [/code]
    However, then $frlink beomes: $super//frlink
    and $frstr becomes $super//frstr

    If this causes errors due to result-tree-fragments, then you might need to use exsl:node-set($super//frlink) or msxsl:node-set($super//frlink)

    Comment

    Working...