Copied to clipboard

Flag this post as spam?

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


  • Frederik T 242 posts 373 karma points
    Sep 08, 2011 @ 16:44
    Frederik T
    0

    Excluding parents when picking random nodes

    Ok, so the site im working on, has a "mega menu" as the top navigation. There are several sub section, and when you mouse over a section, the mega menu drops down and shows the content.

    One of the features is showing a random article from that section. It works, but the only problem is sometimes it also shows just a sub-sub-section which it shouldnt.

    Example of the structure:

    Home
    -Section 1
    --sub-section 1a
    ---article
    ---article
    --sub-section 1b
    ---sub-sub-section 1b
    ----article
    ----article
    -Section 2
    the same

    So the mega menu dropdown for say, Section 1, the random article picker should link ONLY to articles and ignore linking to sub sections.

    Here is my code so far:

    <?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:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:rdm="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:so.user="urn:so.user" xmlns:so.config="urn:so.config" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
      exclude-result-prefixes="msxml msxsl rdm umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets so.user so.config PS.XSLTsearch ">


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

    <xsl:param name="currentPage"/>
    <xsl:param name="siteRoot" select="umbraco.library:GetXmlNodeById(/macro/siteRoot)" />

    <msxsl:script language="JavaScript" implements-prefix="rdm">
        function Random(r){return Math.random()}
    </msxsl:script>
        
    <xsl:template match="/">

      <xsl:for-each select="$siteRoot/child::*/descendant::*[@isDoc][not(umbracoNaviHide = 1)]">
        <xsl:sort select="rdm:Random(1)" order="descending" />
        <xsl:if test="position() &lt;= 1">
          <h6 class="green"><xsl:value-of select="current()/parent::*[@isDoc]/@nodeName"/></h6>
          <ul class="list mm-list green">
            <li class="feature">
              <a href="{umbraco.library:NiceUrl(@id)}">
                <img src="/ImageGen.ashx?image={current()/outputPictures[position()=1]/DAMP[@fullMedia]/mediaItem/Image/umbracoFile}&amp;width=220&amp;height=130&amp;constrain=false" width="220" height="130"  />
                <h5><xsl:value-of select="articleHeading"/></h5>
                <p><xsl:value-of select="umbraco.library:TruncateString(artikel-Teasertekst,170,'...')"/></p>
              </a>
            </li>     
          </ul>
        </xsl:if>
      </xsl:for-each>
      
    </xsl:template>

    </xsl:stylesheet>

     

    I hope my description is sufficient.

  • Richard 146 posts 168 karma points
    Sep 08, 2011 @ 16:50
    Richard
    0

    Frederick,

    Where you have the restriction of

    [not(umbracoNaviHide = 1)]

    add one for the level in the site structure, which is @level, probably 4 so [@level < 4], so you do not list items within the sub-section. Check the umbraco.config file for the level of items in the sub-section.

    Richard

  • Frederik T 242 posts 373 karma points
    Sep 08, 2011 @ 16:55
    Frederik T
    0

    I already navigate a level or two down, but the articles are spread over two different levels. Here is the most correct navigation tree i can re-create:

    Main (lvl 1)
    -Section 1 (lvl 2)
    --Sub-Section 1a (lvl 3)
    ---Sub-sub-section 1aa (lvl 4)
    -----Article (lvl 5)
    -----many more articles
    ---Sub-sub-section 1ab (lvl 4)
    -----Articles (lvl 5)
    --Sub-Section 1b (lvl 3)
    ---Article (lvl 4)
    ---More articles (lvl 4)

     This is how i tried to get around the problem:

    <xsl:for-eachselect="$siteRoot/child::*/descendant::*[@isDoc][not(umbracoNaviHide = 1)]">

    Hope it makes sense. My code picks a random article from section 1, but it also picks a section itself sometimes.

  • Euan Rae 105 posts 135 karma points
    Sep 08, 2011 @ 17:08
    Euan Rae
    0

    Are the articles/sections/subsections different doc types?  If so, you should be able to use name()='doctypename'. something like:

    <xsl:for-eachselect="$siteRoot/child::*/descendant::*[@isDoc][not(umbracoNaviHide = 1)][name()='articleDocType']">

     

  • Richard 146 posts 168 karma points
    Sep 08, 2011 @ 18:30
    Richard
    0

    Frederick,

    I was thinking that you could do

    <xsl:for-eachselect="$siteRoot/child::*/descendant::*[@isDoc][not(umbracoNaviHide = 1)] [@level > 3]">

    So you only get the articles, or do as Euan suggested to only get items of the document type (which is the way I would recommend) if you have a specific document type for your articles.

    Richard

  • Frederik T 242 posts 373 karma points
    Sep 08, 2011 @ 20:30
    Frederik T
    0

    As crazy as it seams, the articles consists of around 10 or so different document types. Yeah, its crazy, and its even crazier they are on different levels and what not. Either way, thats why i thought it would be easier the other way around. I tried excluding hte document types that the sections consist of, but it turned out the articles inherited from them so they were ALL excluded. Sigh... I will try your solutions tomorrow when i get back to my workplace again.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 08, 2011 @ 20:34
    Chriztian Steinmeier
    0

    Hi Frederik,

    If I understand you correct, you only want to pick a random node of type "Article", right? - So that's exactly what you should ask for with the XPath:

    <xsl:for-each select="$siteRoot//Article[not(umbracoNaviHide = 1)]">

    - include as much as you know, e.g. if they're really at any level below the first Section type (or whatever it's called):

    <xsl:for-each select="$siteRoot/Section//Article[not(umbracoNaviHide = 1)]">
    

    (Article and Section are of course DocumentType Aliases, so substitute your own actual names...)

    EDIT: Just saw your previous reply, so I can see it's a bit more involved... ok, we'll just need to find something (a property maybe?) that's unique to all your "Article" types

    /Chriztian

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Sep 08, 2011 @ 20:39
    Chriztian Steinmeier
    1

    - to follow up on that - could the articleHeading property maybe be what you need?

    If so, you could select all descendants that has that property:

    <xsl:for-each select="$siteRoot/*[@isDoc]//*[articleHeading][not(umbracoNaviHide = 1)]">
    

    /Chriztian

  • Frederik T 242 posts 373 karma points
    Sep 09, 2011 @ 09:24
    Frederik T
    0

    Now thats a clever trick! Thank you for pointing that out. Its so simple yet i would never in a million years think about it.

    Ive tested it several times and it looks like it ignores linking to the sections and now just the articles. Very clever indeed. Thank you for your time.

Please Sign in or register to post replies

Write your reply to:

Draft