8

I have a basic condition that checks if a variable is empty and if it is set the variable to a specific value like so.

<xsl:variable name="PIC" select="avatar"/>

<xsl:choose>
  <xsl:when test="avatar !=''">
    <xsl:variable name="PIC" select="avatar"/>
  </xsl:when>
  <xsl:otherwise>
    <xsl:variable name="PIC" select="'placeholder.jpg'"/>
  </xsl:otherwise>
</xsl:choose>

Basically var PIC is set to whatever avatar returns. Then a test is carried to check if if it's not empty and assigned to var PIC and if it is empty a value placeholder.jpg is added to var PIC instead.

Now for some reason I keep getting the following warning

A variable with no following sibling instructions has no effect

Any ideas on what I am doing wrong here?

BaconJuice
  • 3,739
  • 13
  • 56
  • 88

2 Answers2

15

Variables are immutable in XSLT, and cannot be changed once set. The variable declarations in the xsl:choose are simply new declarations that at local in scope to the current block. (They are said to "shadow" the initial variable).

What you need to do is this...

<xsl:variable name="PIC">
   <xsl:choose>
     <xsl:when test="avatar !=''">
        <xsl:value-of select="avatar"/>
     </xsl:when>
     <xsl:otherwise>
       <xsl:value-of select="'placeholder.jpg'"/>
     </xsl:otherwise>
   </xsl:choose>
</xsl:variable>
Tim C
  • 70,053
  • 14
  • 74
  • 93
  • Learned something new today, thank you for your detailed explanation behind your example, appreciate it Tim. – BaconJuice Jul 20 '16 at 14:22
0

Use a single xsl:variable as <xsl:variable name="PIC" select="if (avatar != '') then avatar else 'placeholder.jpg'"/>.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110