Copied to clipboard

Flag this post as spam?

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


  • SaphuA 124 posts 87 karma points
    Sep 27, 2009 @ 21:07
    SaphuA
    0

    useIfEmpty alternative for xslt

    Hi there,

    A while back someone suggested the following template code for me which I've been using quite a while:

    <umbraco:Item field="alternativeTitle" useIfEmpty="pageName" runat="server"></umbraco:Item>

    It uses the alternative title if it's set, otherwise it will use the page name instead.

    Now I would like to do the same in xslt, is this possible?

    Thanks!

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 27, 2009 @ 21:15
    Lee Kelleher
    1

    Hi SaphuA,

    You could write an XSLT extension to handle this in a single line, but to do it natively in XSLT, you can use an <xsl:choose>:

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

    Cheers, Lee.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Sep 27, 2009 @ 21:16
    Jan Skovgaard
    1

    Hi

    Yes it's possible to do with XSLT as well.

    You simply just make a choose section, which could look like this

    <xsl:choose>
    <xsl:when test="string-length('yourproperty') &gt; 0"><xsl:value-of select="yourproperty" /></xsl:when>
    <xsl:otherwise><xsl:value-of select="youralternativeproperty" /></xsl:otherwise>
    </xsl:choose>

    In the xsl:when you check if your primary field has any data. If it has you output the data. If it's empty the data from the alternative field is fetched instead.

    Hope this makes sense.

    /Jan

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Sep 27, 2009 @ 21:16
    Jan Skovgaard
    0

    Once again I'm just too slow...:)

  • Lee Kelleher 4020 posts 15802 karma points MVP 13x admin c-trib
    Sep 27, 2009 @ 21:18
    Lee Kelleher
    0

    @Jan, you know I was thinking when I posted mine... "wonder if anyone else is answering this at the same time?" ;-D  (I've voted for you!)

  • SaphuA 124 posts 87 karma points
    Sep 28, 2009 @ 13:00
    SaphuA
    0

    Thanks for the replies. I marked Lee's post as answe since he was faster and I personally like casting to a string better than using string-length, although that isn't based on anything.

    Thanks!

Please Sign in or register to post replies

Write your reply to:

Draft