Copied to clipboard

Flag this post as spam?

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


  • Erik 2 posts 32 karma points
    Nov 14, 2013 @ 02:36
    Erik
    0

    Get 'grandparent' properties from child nodes

    I thought this would be simple enough but I'm having issues. New to XSLT.

    For the site structure I have:

    Main Site (portal site to display content from Sub Sites)

    ---- Sub Site 1 (has properties I'd like to access from News Article)

    ------- News Section

    ---------- News Article

    ---- Sub Site 2 (has properties I'd like to access from News Article)

    ------- News Section

    ---------- News Article

    I'm trying to list all News Articles on the Main Site. Each News Article should then have an indicator of the Sub Site it's coming from. In this case I have a property for a CSS class on each Sub Site document type. I'll use that to style content coming from each Sub Site.

    Below is what I've tried so far:

    <xsl:for-each select="$currentPage/SiteArea/umbNewsArea/umbNewsArticle [@isDoc and string(umbracoNaviHide) != '1']">
    
            <xsl:if test="position() &lt;= $numberOfItems and position() != 1">
          <li>
            <h4>
              <a href="{umbraco.library:NiceUrl(@id)}">
                <xsl:value-of select="@nodeName"/>
              </a>
            </h4>
            <p>
              <xsl:choose>
                <xsl:when test="string($excerptLength) != '0'">
                  <xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(introduction), number($excerptLength), '...')" disable-output-escaping="yes"/>
                </xsl:when>
                <xsl:otherwise>
                  <xsl:value-of select="introduction" disable-output-escaping="yes"/>
                </xsl:otherwise>
              </xsl:choose>
            </p>
            <small>
              <xsl:value-of select="umbraco.library:ShortDate(@createDate)"/>
            </small>
    
                      <span>                 
                        <xsl:attribute name="class">
                            <xsl:value-of select="$currentPage/SiteArea/BackgroundColorCssClass"/>
                        </xsl:attribute>
                          <xsl:value-of select="$currentPage/SiteArea/@nodeName"/>                          
                     </span>
    
          </li>
        </xsl:if>
    
      </xsl:for-each>
    

    This is giving me only Sub Site 1's properties. Is there something I'm missing to get News Articles to get their 'grandparent' property values?

    Any help would be appreciated.

    Thank you! Erik

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 14, 2013 @ 09:40
    Chriztian Steinmeier
    100

    Hi Erik,

    Here's my take on that - the thing you're missing is just that you need to get to the SiteArea node through the current node of the loop, not using $currentPage (which will always be the MainSite node where you're running the macro, right?).

    I'm using a match template, but the result should be the same:

    <xsl:param name="currentPage" />
    
    <!-- Wherever you are, go to the "Main Site" node -->
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
    <!-- Grab articles to show -->
    <xsl:variable name="newsArticles" select="$siteRoot/SiteArea/umbNewsArea/umbNewsArticle[not(umbracoNaviHide = 1)]" />
    
    <xsl:template match="/">
        <xsl:apply-templates select="$newsArticles[position() &lt;= $numberOfItems and position() &gt; 1]" />
    </xsl:template>
    
    <!-- Template for an article -->
    <xsl:template match="umbNewsArticle">
        <!-- Grab this articles's SiteArea node -->
        <xsl:variable name="mySection" select="ancestor::SiteArea" />
        <li>
            <h4><a href="{umbraco.library:NiceUrl(@id)}"><xsl:value-of select="@nodeName"/></a></h4>
            <p>
                <xsl:choose>
                    <xsl:when test="$excerptLength &gt; 0">
                        <xsl:value-of select="umbraco.library:TruncateString(umbraco.library:StripHtml(introduction), $excerptLength, '...')" disable-output-escaping="yes" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:value-of select="introduction" disable-output-escaping="yes"/>
                    </xsl:otherwise>
                </xsl:choose>
            </p>
            <small>
                <xsl:value-of select="umbraco.library:ShortDate(@createDate)"/>
            </small>
            <span class="{$mySection/BackgroundColorCssClass}">
                <xsl:value-of select="$mySection/@nodeName" />
            </span>
        </li>
    </xsl:template>
    

    /Chriztian

  • Erik 2 posts 32 karma points
    Nov 14, 2013 @ 21:02
    Erik
    0

    Brilliant! Very much appreciated.

    I definitely need to wrap my head around this a little more. I see the benefit with xsl:template. Just haven't gotten it yet.

    Thanks again, Erik

Please Sign in or register to post replies

Write your reply to:

Draft