• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2386
  • Last Modified:

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

0
JohnSixkiller
Asked:
JohnSixkiller
  • 4
  • 2
1 Solution
 
Geert BormansInformation ArchitectCommented:
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'}">
0
 
Geert BormansInformation ArchitectCommented:
sorry, drop the last line
0
 
JohnSixkillerAuthor Commented:
Thank you for quick response, can you give me an example of XPATH2 version?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Geert BormansInformation ArchitectCommented:
you just need lowercase, the rest fits
    <div id='lr-{@id}' style="display:{if(@expanded='yes') then ('block') else ('none')}"/>
0
 
JohnSixkillerAuthor Commented:
Ok, thanks for your time.
0
 
Geert BormansInformation ArchitectCommented:
welcome
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now