Copied to clipboard

Flag this post as spam?

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


  • Connie DeCinko 931 posts 1160 karma points
    Apr 16, 2015 @ 18:58
    Connie DeCinko
    0

    Value of page property, if empty then parent

    It's been awhile since I wrote XSLT so I'm a bit rusty. I have a property in my Document Type that holds a string. I can get that string, no problem with

    $currentPage/mobileURL
    

    Now, if that property on the current page is empty, I want to go back up the tree until I find a value. I thought this worked in the past, but now it won't, gives me no value.

    $currentPage/ancestor-or-self::*/mobileURL
    

    [Umbraco v 4.7.0]

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 16, 2015 @ 19:05
    Dennis Aaen
    0

    Hi Connie,

    I think that  you can do something like this:

    <xsl:choose>
        <xsl:when test="$currentPage/mobileURL !=''">
            <xsl:value-of select="$currentPage/mobileURL" />
        </xsl:when>
        <xsl:otherwise>
            <xsl:value-of select="$currentPage/ancestor-or-self::*[@isDoc]/mobileURL" />
        </xsl:otherwise>
    </xsl:choose>

    Hope this helps,

    /Dennis

  • Connie DeCinko 931 posts 1160 karma points
    Apr 16, 2015 @ 19:10
    Connie DeCinko
    0

    Not quite working. On the page where I set mobileURL it works. On any child pages however, I get no value.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 16, 2015 @ 19:28
    Dennis Aaen
    0

    Hi Connie,

    That's quite interesting, I have just tried setup a test site where I add a value on a child page, and on the parent page and on the pages that doesn't have a value on the mobileURL field, it takes it from the parent page, else it take it from the current

    /Dennis

  • Amir Khan 1282 posts 2739 karma points
    Apr 16, 2015 @ 19:44
    Amir Khan
    0

    It's been a while for me as well, but don't you just put recursive="true" when you put the macro in your template?

    -Amir

  • Connie DeCinko 931 posts 1160 karma points
    Apr 16, 2015 @ 19:52
    Connie DeCinko
    0

    If I use parent, as in

    <xsl:value-of select="$currentPage/parent::*[@isDoc]/mobileURL" />
    

    I get a value. Just not getting anything from ancestor-or-self.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 16, 2015 @ 19:52
    Dennis Aaen
    101

    Hi Connie,

    As Amir said you could try to make the macro recursive, by doing this:

    <umbraco:macro alias="nameofmacro" pagevalue="[$mobileURL]" runat="server"/>

    You can find the documentation here: https://our.umbraco.org/wiki/reference/templates/umbracomacro-element/macro-parameters/advanced-macro-parameter-syntax

    Remember to change the alias of the macro so it match your case.

    Hope this can help you,

    /Dennis

  • Jan Skovgaard 11280 posts 23678 karma points MVP 10x admin c-trib
    Apr 16, 2015 @ 20:02
    Jan Skovgaard
    1

    Hi Connie

    It's not the recursive thing on the macro you're after. It should be possible to use ancestor-or-self. But could you please share your full code? And perhaps a screemdump or something that illustrates your structure?

    Don't know if it helps you but perhaps you can benefit from using the nice xpath axes visualizer by XSLT wizard Chriztian Steinmeier here http://pimpmyxslt.com/axesviz.aspx?contextNode=1073&axis=ancestor-or-self#screen

    Looking forward to hearing more from you.

    /Jan

  • Connie DeCinko 931 posts 1160 karma points
    Apr 16, 2015 @ 20:15
    Connie DeCinko
    0

    I was able to get it work using the method Dennis prescribed. That's how I did something similar several years ago, just did not recall the why or how. The ancestor-or-self should work, but it did not. The recursive works like a charm. Good enough until we get time to upgrade to Umbraco 7 next year.

  • Dennis Aaen 4499 posts 18254 karma points admin hq c-trib
    Apr 16, 2015 @ 20:43
    Dennis Aaen
    1

    Hi Connie,

    Happy that you find a way to solve the case. And as you say then you can dig into it further when you have time or wait until you get time to upgrade to Umbraco 7.

    I think you should mark the question as solved so if other comes across this post, then they can go directly to the solution that worked for you.

    /Dennis

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Apr 16, 2015 @ 22:20
    Chriztian Steinmeier
    1

    Hi all,

    Just to explain the ancestor-or-self:: bit:

    As you can see from Jan's link (thanks Jan :-), using the ancestor-or-self:: axis returns all of the nodes - but <xsl:value-of /> on a nodeset will only return the string value of the first node in that set, which in this case is $currentPage (the ancestor axes selects in reverse order).

    The way to get an Umbraco recursive value with XSLT is to select all the nodes from the ancestor-or-self:: axis that have a value in the mobileURL property, and then take the first of those and output its mobileURL - like this:

    <xsl:value-of select="$currentPage/ancestor-or-self::*[normalize-space(mobileURL)][1]/mobileURL" />
    

    Hope that helps explain why you couldn't get it to work,

    /Chriztian

  • Connie DeCinko 931 posts 1160 karma points
    Apr 16, 2015 @ 22:43
    Connie DeCinko
    0

    Chriztian, that's similar to what I was thinking. I was thinking the self in ancestor-or-self is empty but is the first thing returned in the collection of values.

Please Sign in or register to post replies

Write your reply to:

Draft