Copied to clipboard

Flag this post as spam?

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


  • Neil 63 posts 105 karma points
    Apr 13, 2013 @ 16:04
    Neil
    0

    how to set a variable if a node's ancestor has a certain name

    I need to set the value of a variable based on whether one of the ancestors of the current node has a certain name, and if the parent of that ancestor with the certain name has a certain name.  How do I traverse the ancestors of the current node to test these two conditions?

    I tried this but the because the leveltest variable is out of scope in the second for-each loop.  Also, I don't even know if i'm getting the syntax right for matching the nodename of the ancestors.  New to xslt so any help would be appreciated.

    <xsl:for-each select="$currentPage/ancestor::*">
    <xsl:variable name="leveltest">
    <xsl:choose>
    <xsl:when test="@Nodename = 'Growth Equity' and parent::node/@nodeName = 'Home'">2</xsl:when>
    <xsl:otherwise>1</xsl:otherwise>
    </xsl:choose>
    </xsl:variable>
    </xsl:for-each>

    <xsl:for-each select="$currentPage/ancestor::* [@level &gt; $leveltest]">
    <a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></li></xsl:if></xsl:for-each>

     

     

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 13, 2013 @ 17:11
    Chriztian Steinmeier
    0

    Hi Neil,

    Which version of Umbraco are you using?

    Are you trying to render a breadcrumb of some sorts but needing to adjust it on a specific branch of the site, maybe?

    /Chriztian 

  • Neil 63 posts 105 karma points
    Apr 13, 2013 @ 18:59
    Neil
    0

    Hi Chriztian,

    Umbraco 4.7. And yes, trying to render a breadcrumb that needs to be adjusted for a branch. I've got a client who wants to build a microsite with separate navs and breadcrumb trail.  I tried putting both the main site and the microsite at the root level, but naming subnodes the same thing (a necessity), such as Careers, always renders the links to the main site's Careers page.  Of course, I also tried to not hide the top level path, but then I get the ugly "home" root directory on the main site, which is undesirable.

    My best solution was to put the mircosite at the L2 depth, and I can get breadcrumb and navs to work right as long as I change the level at which I start when in the microsite's branch.  It works fine when I'm at the root of the microsite on that branch, but the child nodes of that root don't know it's there unless I traverse up to it, matching names as I go until I find it, and until I make sure it's the microsite by checking to see if the parent's name is 'Home.'

     

  • Chriztian Steinmeier 2800 posts 8791 karma points MVP 8x admin c-trib
    Apr 13, 2013 @ 19:34
    Chriztian Steinmeier
    100

    Allright - it makes sense now;

    Be sure to express this in the code (variable names etc.) - something like this should do:

    <xsl:variablename="isMicroSite"select="boolean($currentPage/ancestor-or-self::*[@nodeName = 'Growth Equity'][../@nodeName = 'Home'])"/>
    <xsl:variablename="topLevel"select="1 + number($isMicroSite)]"/>

    <xsl:for-eachselect="$currentPage/ancestor::*[@level &gt; $topLevel]">
           
    <li>
                   
    <ahref="{umbraco.library:NiceUrl(@id)}"><xsl:value-ofselect="@nodeName"/></a>
           
    </li>
    </xsl:for-each>

    /Chriztian

  • Neil 63 posts 105 karma points
    Apr 13, 2013 @ 20:21
    Neil
    0

    That worked perfectly.  I've been coding a long time, but am still learning the xslt syntax.  So, when you do the initial select for the isMicroSite variable, that call traverses the branch based on ancestor-or-self directive?  I think that's where I was getting confused.  I was thinking I had to loop through it somehow with a for-each, where instead you just do the select.  Thanks for the help!

Please Sign in or register to post replies

Write your reply to:

Draft