Copied to clipboard

Flag this post as spam?

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


  • Brenton Mumford 19 posts 40 karma points
    Sep 07, 2010 @ 21:27
    Brenton Mumford
    0

    Basic "Related Items" List in XSLT in v 4.0

    Hey, folks,

    Brand new to Umbraco, and XSLT, for that matter.  Trial and error is fun, and I've learned a lot, but I'm stuck on this problem.

    I'm wanting to place a list of 5 related articles on the same page as another article with the same category.  Initially I wanted to have the category act more like a tag (allowing multiple tags on the same article) but I abandoned that in favor of a one-per-post category due because I couldn't figure out how to do it.

    So, what I've got so far is below:

    <xsl:template match="/">
    
        <!-- gets other pages with the same "News Category" -->
        <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=4]/descendant-or-self::node [@level=5]">
    
            <!-- establishes current page's category -->
            <xsl:if test="current()/data[@alias='newsTag'] = 'Specific Category Name'">
    
                <!-- limits to less than 5 responses -->
                <xsl:if test="position() &lt; 6">
    
                    <p><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></p>
    
                </xsl:if>
    
            </xsl:if>
    
        </xsl:for-each>
    
    </xsl:template>

    So, here are my issues:  

    1)  Now, this brings back 5 or less articles that have the tag name as explicitly specified as 'Specific Category Name'.  The reason I say less is that if there are any articles more recent that do not have that same 'Specific CategoryName,' it will just skip them until it gets to the 5th overall article, as opposed to the 5th article with the 'Specific Category Name.'  I would like the latter, of course.

    2)  I need to be able to replace that 'Specific Category Name' with the tag associated with the page, using some bit of code that escapes me so far.  I tried setting a variable to get the tag name and then plug it in, but clearly I'm doing that incorrectly, because I wasn't able to get it to work.

    I know the Blog package would make some of this functionality present, but we're on 4.0 and some in my organization are reluctant to upgrade to 4.5.  I tried examining the .xslt files in that package and gleaning what I could, but I was not able to find the solution therein.

    If someone is aware of a way to successfully accomplish a simple 'tagging' scenario without the aid of the Blogging package, I would be extremely interested in that, as well.  I was not able to track one down in my searching.

  • Jeff Grine 149 posts 189 karma points
    Sep 07, 2010 @ 23:39
    Jeff Grine
    0

    Can't help much with tagging in general but for problem 1, you should put the criteria for the category name in your for-each select. Position() will always give the overall position in the current set, so you need to limit the set to only items where newsTag = Category Name. For problem 2, sounds like you just need a property on the page for your category, then the criteria becomes data[@alias='newsTag'] = $currentPage/data[@alias='categoryProperty'], or something like that. Hope that helps some. If the select gets a little overwhelming, try putting your current select in a variable, then write another select off of that for the for-each. Sometimes helps to break it up.

  • Brenton Mumford 19 posts 40 karma points
    Sep 08, 2010 @ 21:02
    Brenton Mumford
    0

    Jeff - thanks so much for your help.  I've made it considerably further thanks to your thoughts.  Here's what I have now:

    
    
    
    
        
    
    
    
            
    
    
                
    
    
                
    
    
                    

    Problem number two is solved, hooray!  Problem number one (the one with the 5 results) is still there, however; I thought, if I read Jeff's comments correctly earlier, if I changed 

    
    

    to

    
    

    that would take care of it.  (Just adding the path to the data type itself, rather than the node.)  However, when I do that, my results disappear.  I have a feeling it's something simple and will make me feel dumb, but it won't be the first time for that!

  • Brenton Mumford 19 posts 40 karma points
    Sep 08, 2010 @ 21:23
    Brenton Mumford
    0

    Gack!  I tried to edit to enhance clarity - and my code went away.  At least, I can't see it.  Just in case you can't, either, here's the whole thing again:

    *************

    Jeff - thanks so much for your help.  I've made it considerably further thanks to your thoughts.  Here's what I have now:

    <xsl:template match="/">
    
      <xsl:variable name="Tag" select="$currentPage/data[@alias='newsTag']"/>
      <xsl:variable name="Name" select="$currentPage/data[@alias='articleName']"/>
    
        <!-- gets other pages with the same "News Tag" -->
    
        <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=4]/descendant-or-self::node [@level=5]">
    
            <!-- matches other pages with current page's tag -->
            <xsl:if test="data[@alias='newsTag'] = $Tag">
    
                <!-- prevents current article from reprinting in 'related items' list -->
                <xsl:if test="data[@alias='articleName'] != $Name">
    
                <!-- limits to less than X responses -->
                <xsl:if test="position() &lt; 6">
    
                    <p><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></p>
    
                </xsl:if>
    
            </xsl:if>
            </xsl:if>
        </xsl:for-each>
    
    </xsl:template>

    Problem number two is solved, hooray!  Problem number one (the one with the 5 results) is still there, however; I thought, if I read Jeff's comments correctly earlier, if I changed 

    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=4]/descendant-or-self::node [@level=5]">
    

    to

    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=4]/descendant-or-self::node [@level=5]/data[@alias='newsTag']">
    

    that would take care of it.  (Just adding the path to the data type itself, rather than the node.)  However, when I do that, my results disappear.  I have a feeling it's something simple and will make me feel dumb, but it won't be the first time for that!

  • Jeff Grine 149 posts 189 karma points
    Sep 08, 2010 @ 21:29
    Jeff Grine
    0

    lol - cleared it right up!

    What you changed it to will only select the data with that alias - so you'll only get the actual categories. You need it inside the criteria (brackets). Something like:

    <xsl:for-each select="$currentPage/ancestor-or-self::node [@level=4]/descendant-or-self::node [@level=5 and data[@alias='newsTag']=$Tag]">

  • Brenton Mumford 19 posts 40 karma points
    Sep 08, 2010 @ 21:37
    Brenton Mumford
    0

    It's a winner!  Thanks, Jeff!  I'm sure the reason that worked will sink in in the middle of the night.

Please Sign in or register to post replies

Write your reply to:

Draft