2

I am comparing two variable in xsl. When I do

<p>Language:<xsl:value-of select="$LANGUAGE_EN"/>=<xsl:value-of select="$CONTEXT_LANGUAGE"/></p>

It Output en=en

But when I compare Using:

<xsl:choose>
   <xsl:when test='string($CONTEXT_LANGUAGE) = string($LANGUAGE_EN)'>
      <p>English Language</p>
   </xsl:when>
   <xsl:otherwise>
      <p>French Language</p>
   </xsl:otherwise>                
</xsl:choose>   

It always returns French Language, but it should return English Language.

Can someone please help me on this, I lost my whole day on this?

Vinoth Krishnan
  • 2,925
  • 6
  • 29
  • 34
Emran
  • 33
  • 1
  • 6
  • 2
    Your test expression should've worked. Post minimal XML & XSLT sample to reproduce the problem – har07 Apr 02 '16 at 07:24
  • It would be easier to diagnose the issue if you posted a sample of your XML. The issue is likely extra whitespace in your XML that you are not seeing in the HTML output. – Mads Hansen Apr 02 '16 at 14:32
  • I have defined the variable like this: en fr – Emran Apr 02 '16 at 15:11

2 Answers2

1

It is likely that your values have leading and/or trailing whitespace that you are not seeing; especially if viewing the rendered HTML in a browser. In your first example, append a character before and after the values:

<p>Language:*<xsl:value-of select="$LANGUAGE_EN"/>*=*<xsl:value-of select="$CONTEXT_LANGUAGE"/>*</p>

You could also test the string-length()

If the difference is leading or trailing whitespace, you can use the normalize-space() function to get rid of them when comparing values:

<xsl:choose>
    <xsl:when test='normalize-space($CONTEXT_LANGUAGE) = normalize-space($LANGUAGE_EN)'>
        <p>English Language</p>
    </xsl:when>
    <xsl:otherwise>
        <p>French Language</p>
    </xsl:otherwise>                
</xsl:choose> 
Mads Hansen
  • 63,927
  • 12
  • 112
  • 147
  • Language: *en*=*en* – Emran Apr 02 '16 at 15:09
  • Print the `*` **before** and **after** each value. It appears that there is a space before the first `en` in your comment...Also, what are the `string-length()` values? – Mads Hansen Apr 02 '16 at 17:52
  • Thank you for the normalize-space($VAR) suggestion. Actually, trailings spaces depends on XSLT engine !!! It saved me hours... – bruno777 Sep 06 '17 at 13:43
0

Whatever you are doing is correct but thing is don't try to compare values directly instead try to store them in 2 different variables and compare the variables. You can use xsl variable as:

<xsl:variable
 name="name"
 select="expression">
</xsl:variable>

Try to assign 2 different variables for the 2 values : $CONTEXT_LANGUAGE and $LANGUAGE_EN. Now, try to print both the variables and cross verify them on HTML page if you are getting values what you have assigned then you are on the right track and now try your loop <'xsl:choose'>. This should work fine if not try to create an if loop and again check if <'xsl:if'> works fine. According, to me both should work.

Please vote if the solution is useful.

Thanks!

Ashraf.Shk786
  • 618
  • 1
  • 11
  • 23