Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • kentiler 76 posts 69 karma points
    Feb 24, 2010 @ 02:57
    kentiler
    0

    If/Then or Switch/Select functionality with XSLT?

    Hi,

    I have a custom datatype that is a drop down list where users will be able to specify certain look and feel attributes on the page properties.

    There are roughly 10 items in the drop down list.  I am trying to write the code to determine the selected value from this datatype (called titleColorPicker), but I'm having trouble since Xslt doesn't have an else feature.  I could go with 10 if statements, but is there a more efficient way to do this?

    Thank you!

    --Kent Iler

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 24, 2010 @ 03:16
    Lee Kelleher
    2

    Hi Kent,

    You can use the <xsl:choose> element. For example:

    <xsl:choose>
        <xsl:when test="string(data[@alias='titleColorPicker']) != ''">
            <xsl:value-of select="data[@alias='titleColorPicker']" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:text>default value</xsl:text>
        </xsl:otherwise>
    </xsl:choose>

    It's similar logic to an if/else statement (in procedural programming), but you can have multiple <xsl:when> conditions in the same <xsl:choose>.

    Good luck, Lee.

  • kentiler 76 posts 69 karma points
    Feb 24, 2010 @ 16:21
    kentiler
    0

    Thanks, Lee!  I'll give that a shot!

  • kentiler 76 posts 69 karma points
    Feb 25, 2010 @ 08:33
    kentiler
    0

    I think I want to change the logic a bit and just put the value of the drop down list as the ID in the div statement.  I'm having a bit of trouble with this.  For example, the property is named titleColor (it's a custom DDL), but this doens't work.

    <div class="grid_12" id="{data[@alias = 'titleColor']}">

    Any ideas?

    Thanks!

    --Kent

  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 25, 2010 @ 08:50
    Kim Andersen
    0

    Hi Kent

    Try something like this:

    <div class="grid_12">
        <xsl:attribute name="id"><xsl:value-of select="$currentPage/data[@alias='titleColor']" /></xsl:attribute>
    
    </div>

    Hope this makes sense to you.

    /Kim A

  • jonok 297 posts 658 karma points
    Feb 25, 2010 @ 08:55
    jonok
    0

    Hi Kent, you should be able to add an id attribute like this:

    <div class="grid_12">
    <xsl:attribute name="id">
    <xsl:value-of select="data[@alias='titleColorPicker']" />
    </xsl:attribute>

     

  • Petr Snobelt 923 posts 1535 karma points
    Feb 25, 2010 @ 09:41
    Petr Snobelt
    0

    BTW: xsl:if is also available...

  • kentiler 76 posts 69 karma points
    Feb 26, 2010 @ 06:31
    kentiler
    0

    I appreciate all the help, but still having trouble.  I've tried:

    <div class="grid_12">
       
    <xsl:attribute name="id"><xsl:value-of select="$currentPage/data[@alias='titleColorPick']" /></xsl:attribute>
    </div>

    and

    <div class="grid_12">
    <xsl:attribute name="id">
           
    <xsl:value-of select="data[@alias='titleColorPick']" />
    </xsl:attribute>

     

    But both approaches still end up with an empty value in the ID tag:

     

    <div class="grid_12" id="" />
    

    I have verified the name of the property is now titleColorPick (changed it to something different from the XSLT file name, and also verified that the current page has a value set.

    Any ideas?

    Thanks!

    --Kent

  • Petr Snobelt 923 posts 1535 karma points
    Feb 26, 2010 @ 08:25
  • kentiler 76 posts 69 karma points
    Feb 26, 2010 @ 20:47
    kentiler
    0

    I checked that link, Petr, but it doesn't seem to apply.  Here's the code that I have for the titleColorPick:

    <xsl:template match="/">
    <!-- start writing XSLT -->
    <div class="grid_12"><xsl:attribute name="id"><xsl:value-of select="data[@alias='titleColorPick']" /></xsl:attribute></div>
    </xsl:template>
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 26, 2010 @ 20:55
    Lee Kelleher
    0

    Hi Kent,

    I'm pretty sure Petr is on the right line of thinking here.  Try this modified XSLT:

    <xsl:template match="/">
        <div class="grid_12">
            <xsl:attribute name="id">
                <xsl:value-of select="data[@alias='titleColorPick']" />
            </xsl:attribute>
            <xsl:comment><![CDATA[.]]></xsl:comment>
        </div>
    </xsl:template>

    The problem with empty tags self-closing is that they have no text/content ... hence they self-close.  So if you add a HTML comment in there - then it wont self-close ... and you wont see any visible difference on the rendered webpage in your browser.

    If that doesn't work, then it might be worthwhile posting your entire XSLT? - so that we can get a better understanding of how to solve your problem.

    Cheers, Lee.

  • kentiler 76 posts 69 karma points
    Feb 26, 2010 @ 21:47
    kentiler
    0

    Here's the result from your code, Lee:

     

    <!-- Start Title -->
    <div class="grid_12" id=""><!--.--></div>
    <!-- End Title -->
    

    Here's the items from the custom drop down list - could it be something from the datatype configuration?

    Datebase datatype


    Text Value
    Black 8 Delete
    Blue 9 Delete
    Brown 10 Delete
    Green 11 Delete
    Orange 12 Delete
    Pink 13 Delete
    Purple 14 Delete
    Red 15 Delete
    Turquoise 16 Delete

    Add prevalue
  • kentiler 76 posts 69 karma points
    Feb 26, 2010 @ 21:48
    kentiler
    0

    Here's the full XSLT:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:msxml="urn:schemas-microsoft-com:xslt"
     xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets"
     exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets ">

    <xsl:output method="xml" omit-xml-declaration="yes"/>
    <xsl:param name="currentPage"/>
    <xsl:template match="/">
    <div class="grid_12">
    <xsl:attribute name="id">
    <xsl:value-of select="data[@alias='titleColorPick']" />
    </xsl:attribute>
    <xsl:comment><![CDATA[.]]></xsl:comment>
    </div></xsl:template>
    </xsl:stylesheet>
  • Kim Andersen 1447 posts 2196 karma points MVP
    Feb 26, 2010 @ 21:55
    Kim Andersen
    1

    I think that you should have a $currentPage/ in front of your data[@alias......But you said that you had tried this or?

    Otherwise, try to print the value out in a textarea, to make sure that there is a value in the field. Try this:

    <textarea>
    <xsl:value-of select="$currentPage/data[@alias='titleColorPick']" />
    </textarea>

    Just to make sure. And by the way Kent, which version of Umbraco are you using?

    /Kim A

  • kentiler 76 posts 69 karma points
    Feb 26, 2010 @ 22:44
    kentiler
    0

    That did it!  Thanks to all who helped!

    --Kent

  • Kim Andersen 1447 posts 2196 karma points MVP
    Mar 02, 2010 @ 09:50
    Kim Andersen
    0

    Perfect Kent. Remember to mark one of all the answers in this thread as the solution, so that people know that this issue har been solved :)

    /Kim A

Please Sign in or register to post replies

Write your reply to:

Draft