Copied to clipboard

Flag this post as spam?

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


  • Tegan Snyder 3 posts 23 karma points
    Apr 05, 2011 @ 20:28
    Tegan Snyder
    0

    Conditional Recursion on Related Links

    Each "Content Folder" document type on my site has a helpfulLinks attached to it. I also added a True/False checkbox to the "Content Folders". I have it labled - "Inherit Helpful Links"

    Currently the way I have my XSLT setup is to retrieve the first helpfulLinks it finds as it travels the node tree using:

    <xsl:for-each select="$currentPage/ancestor-or-self::* [helpfulLinks][1] /helpfulLinks/links/link">

    The code above works great but I don't always want to use the first helpfulLinks it finds. I'm trying to code a way to use the "Inherit Helpful Links" checkbox so when it is checked it uses the top most parent's helpfulLinks.

    In the case of the screenshots above. The "Content Folder" labled "Job Openings" has the Inherit box checked. It should be using the helpfulLinks of the "Content Folder" above it labled "Employment".

    I appreciate any help you can provide. Thanks!

    Here is my full XSLT code:

    <?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" xmlns:tagsLib="urn:tagsLib" xmlns:BlogLibrary="urn:BlogLibrary" 
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets tagsLib BlogLibrary ">


      <xsl:output method="xml" omit-xml-declaration="yes" />

      <xsl:param name="currentPage"/>
      <xsl:variable name="propertyAlias" select="string('helpfulLinks')"/>
        
      <xsl:template match="/">
        
      <xsl:variable name="inheritLinks" select="$currentPage/ancestor-or-self::*[inheritHelpfulLinks][1]/inheritHelpfulLinks"/>
      
      <xsl:variable name="countLinks" select="count($currentPage/ancestor-or-self::*[helpfulLinks][1]/helpfulLinks/links/link)"/>  
      <xsl:value-of select="$countLinks"/>
        
      <xsl:choose>
        
        <xsl:when test="$inheritLinks &gt; 0">
          
          need to do something here when inheritLinks is checked to display parent links
          
        </xsl:when>
        
        <xsl:otherwise>

        <ul class="sidebar_nav">
          <xsl:for-each select="$currentPage/ancestor-or-self::* [helpfulLinks][1] /helpfulLinks/links/link">
            <li>
              <xsl:element name="a">
                <xsl:if test="./@newwindow = '1'">
                  <xsl:attribute name="target">_blank</xsl:attribute>
                </xsl:if>
                <xsl:choose>
                  <xsl:when test="./@type = 'external'">
                    <xsl:attribute name="href">
                      <xsl:value-of select="./@link"/>
                    </xsl:attribute>
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:attribute name="href">
                      <xsl:value-of select="umbraco.library:NiceUrl(./@link)"/>
                    </xsl:attribute>
                  </xsl:otherwise>
                </xsl:choose>
                <xsl:value-of select="./@title"/>
              </xsl:element>
            </li>
          </xsl:for-each>
        </ul>
          
        </xsl:otherwise>
        
      </xsl:choose>
        

      </xsl:template>

    </xsl:stylesheet>
  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 05, 2011 @ 21:19
    Lee Kelleher
    1

    Hi Tegan,

    Hopefully I've understood the logic of your related links.  I've rewritten the XSLT, which should make some sense?

    <?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"
        exclude-result-prefixes="msxml umbraco.library">
        <xsl:output method="xml" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage"/>
    
        <xsl:template match="/">
            <xsl:choose>
                <!-- test of the 'inheritHelpfulLinks' is set -->
                <xsl:when test="$currentPage/inheritHelpfulLinks = 1">
                    <!-- apply-templates with recursive 'helpfulLinks' -->
                    <xsl:apply-templates select="$currentPage/ancestor-or-self::*[@isDoc and normalize-space(helpfulLinks)][1]/helpfulLinks" />
                </xsl:when>
                <xsl:otherwise>
                    <!-- otherwise apply-templates on the currentPage's 'helpfulLinks' -->
                    <xsl:apply-templates select="$currentPage/helpfulLinks" />
                </xsl:otherwise>
            </xsl:choose>
        </xsl:template>
    
        <!-- template for 'helpfulLinks' with no links -->
        <xsl:template match="helpfulLinks[not(links)]">
            <p>No links.</p>
        </xsl:template>
    
        <!-- template for 'helpfulLinks' with links -->
        <xsl:template match="helpfulLinks[links]">
            <ul class="sidebar_nav">
                <!-- loop through each of the links -->
                <xsl:for-each select="links/link">
                    <li>
                        <!-- output the external href as default -->
                        <a href="{@link}">
                            <xsl:if test="@type != 'external'">
                                <!-- if its not external, then NiceUrl it! -->
                                <xsl:attribute name="href">
                                    <xsl:value-of select="umbraco.library:NiceUrl(@link)"/>
                                </xsl:attribute>
                            </xsl:if>
                            <xsl:if test="@newwindow = '1'">
                                <!-- test if new window selected -->
                                <xsl:attribute name="target">_blank</xsl:attribute>
                            </xsl:if>
                            <xsl:value-of select="@title" />
                        </a>
                    </li>
                </xsl:for-each>
            </ul>
        </xsl:template>
    
    </xsl:stylesheet>

    Basically the initial template checks if the "inheritHelpfulLinks" is set, then does a recursive check - otherwise it falls back on the $currentPage property. There's a separate template for the "helpfulLinks" ... as you only have to maintain the HTML in a single place then.

    Let me know how it works out.

    Cheers, Lee.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Apr 05, 2011 @ 21:31
    Chriztian Steinmeier
    2

    Great rewrite Lee - and you know exactly what I mean by that :-)

    If I understand Tegan correct, he wants to grab the topmost parent's helpfulLinks, which should be possible with a tiny predicate change:

    <xsl:apply-templates select="$currentPage/ancestor-or-self::*[normalize-space(helpfulLinks)][last()]/helpfulLinks" />

    (Just changing the [1] to [last()] ... )

    /Chriztian 

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Apr 05, 2011 @ 21:37
    Lee Kelleher
    0

    Cool, happy to know you've "got my back" Chriztian! ;-)

    I was tempted to take it one step further and separate out the <link>s into their own template... but didn't want to go too overkill for Tegan.

    Cheers, Lee.

  • Tegan Snyder 3 posts 23 karma points
    Apr 05, 2011 @ 21:56
    Tegan Snyder
    0

    Thanks Chriztian and Lee! I'm wrapping things up for the day, but plan on trying your suggestions tomorrow. I will let everyone know how it works. Wow for my first post in the forums I'm pretty happy :)

  • Tegan Snyder 3 posts 23 karma points
    Apr 05, 2011 @ 22:05
    Tegan Snyder
    0

    Alright I lied.... Im not leaving until I try this...heheeh

    I just tried Chriztian suggestion on my existing code XSLT I just changed the [1] to [last()] and it worked! I will look into the formatting suggestions Lee posted above to get some more ideas too.

    thanks

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Apr 05, 2011 @ 22:18
    Chriztian Steinmeier
    0

    Ha!

    The magic XSLT/Umbraco Magnet has stricken again!

    /Chriztian

     

Please Sign in or register to post replies

Write your reply to:

Draft