Link to home
Start Free TrialLog in
Avatar of JohnSixkiller
JohnSixkillerFlag for Czechia

asked on

XSL XPATH condition in attribute value

Hi, experts

is there any way to use conditions in XSL/XPATH in attribute's value?
I need something like this:

...
<div id='lr-{@id}' style="display:{IF @expanded='yes' THEN 'block' ELSE 'none'}">
...

At this time I have this code:
But with more variables comes lot of code. :(
<xsl:variable name="visibility">
  <xsl:choose>
	  <xsl:when test="@expanded='yes'">
	   <xsl:value-of select="'block'"/>
	  </xsl:when>
	  <xsl:otherwise>
	   <xsl:value-of select="'none'"/>
	  </xsl:otherwise>
 </xsl:choose>
</xsl:variable>

Open in new window

Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

If you would be using XSLT2, you can have the choice in your XPath...
the code would not be far off from what you have in your pseudo code

In XSLT1, you would need the xsl:choose approach
very close to what you did, except that you don't necessarily need the variable, you could use an xsl:attribute

<div id='lr-{@id}'>
   <xsl:attribute name="style=">
          <xsl:text>display:</xsl:text>
          <xsl:choose>
        <xsl:when test="@expanded='yes'">
           <xsl:text>block</xsl:text>
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>none</xsl:text>
        </xsl:otherwise>
          </xsl:choose>
   </xsl:attribute>

{IF @expanded='yes' THEN 'block' ELSE 'none'}">
sorry, drop the last line
Avatar of JohnSixkiller

ASKER

Thank you for quick response, can you give me an example of XPATH2 version?
ASKER CERTIFIED SOLUTION
Avatar of Gertone (Geert Bormans)
Gertone (Geert Bormans)
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, thanks for your time.