Emran
Emran

Reputation: 33

How to compare two variable in xsl?

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?

Upvotes: 2

Views: 5774

Answers (2)

Ashraf.Shk786
Ashraf.Shk786

Reputation: 624

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!

Upvotes: 0

Mads Hansen
Mads Hansen

Reputation: 66714

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> 

Upvotes: 1

Related Questions