Solved

XSL XPATH condition in attribute value

Posted on 2008-10-16
6
2,354 Views
Last Modified: 2013-11-18
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
Comment
Question by:JohnSixkiller
  • 4
  • 2
6 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22729687
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
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22729690
sorry, drop the last line
0
 
LVL 11

Author Comment

by:JohnSixkiller
ID: 22730005
Thank you for quick response, can you give me an example of XPATH2 version?
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 22730034
you just need lowercase, the rest fits
    <div id='lr-{@id}' style="display:{if(@expanded='yes') then ('block') else ('none')}"/>
0
 
LVL 11

Author Closing Comment

by:JohnSixkiller
ID: 31506665
Ok, thanks for your time.
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 22730230
welcome
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now