Copied to clipboard

Flag this post as spam?

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


  • Sarah 40 posts 104 karma points
    Oct 21, 2012 @ 01:14
    Sarah
    0

    Checking a document property in for-each

    Hi guys,

    I'm fairly new to Umbraco and XSLT having only been using them for a personal project since June. I've found the forums and wiki an excellent source of help and until now haven't needed to post anything here. But I am now completely stuck and wonder if any of you could help me. I've got an XSLT macro I have created for my site to populate a "Things you may also like" sort of section on my site.

    To give you some background, my site is basically editorial content made up of Articles and Reviews. I want to populate this "you may also like" section with the 5 most recently posted Articles and Reviews, but NOT if one of those is the article or review the user is currently reading. I hope that makes sense! I've been trying to do it on articleTitle or reviewTitle (properties on the document types) but I'm getting nowhere. Here is the XSLT I have at the moment which almost does what I want. It lists the 5 most recent posts to the site but even if one of those is the page they are currently reading. I've also put in a commented out line that almost worked but not quite. Doing this did remove the article or review currently being read, but also limited the list to just articles or just reviews and I want it to combine both. I really hope this makes sense but feel free to ask as many questions as you need to! Thanks in advance :)

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


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

    <xsl:param name="currentPage"/>
      
      <!-- Input the documenttype you want here -->
      <xsl:variable name="articleTypeAlias" select="string('Article')"/>
      <xsl:variable name="reviewTypeAlias" select="string('Review')"/>
      <xsl:variable name="maxRecentPublished" select="5" />
      <xsl:variable name="root" select="$currentPage/ancestor-or-self::Homepage" />

      <xsl:template match="/">

        <!-- The fun starts here -->

    <!-- <xsl:for-each select="$root/*[@isDoc]/* [name() = $articleTypeAlias and $currentPage/articleTitle != articleTitle or name() = $reviewTypeAlias and $currentPage/reviewTitle != reviewTitle and @isDoc and string(umbracoNaviHide) != '1']"> -->
        <xsl:for-each select="$root/*[@isDoc]/* [name() = $articleTypeAlias or name() = $reviewTypeAlias and @isDoc and string(umbracoNaviHide) != '1']">
          <xsl:sort select="publishedDate" order="descending"/>
          <xsl:if test="position() &lt;= $maxRecentPublished">
            <xsl:if test="name() = $articleTypeAlias">
              <hr width="80%" colour="#D3D3D3"></hr>
              <href="{umbraco.library:NiceUrl(@id)}" class="youMayAlsoTitle">
                <xsl:value-of select="articleTitle"/>
              </a>
            </xsl:if>
            <xsl:if test="name() = $reviewTypeAlias">
              <hr width="80%" colour="#D3D3D3"></hr>
              <href="{umbraco.library:NiceUrl(@id)}" class="youMayAlsoTitle">
                <xsl:value-of select="reviewTitle"/>
              </a>
            </xsl:if>
          </xsl:if>
        </xsl:for-each>
    </xsl:template>

    </xsl:stylesheet>

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 21, 2012 @ 01:30
    Chriztian Steinmeier
    0

    Hi Sarah,

    Here's a way to do that - I've rearranged some bits and pieces, but hopefully it's pretty easy to understand what's going on - otherwise just ask!

    <xsl:param name="currentPage"/>
    <xsl:variable name="maxRecentPublished" select="5" />
    <xsl:variable name="root" select="$currentPage/ancestor-or-self::Homepage" />
    
    <!-- Grab all eligible Articles and Reviews -->
    <xsl:variable name="articlesAndReviews" select="$root/*[@isDoc]/*[self::Article or self::Review][not(umbracoNaviHide = 1)]" />
    
    <xsl:template match="/">
        <!-- Show top 5 excluding the one currently being viewed -->
        <xsl:for-each select="$articlesAndReviews[not(@id = $currentPage/@id)]">
            <xsl:sort select="publishedDate" order="descending" />
            <xsl:if test="position() &lt;= $maxRecentPublished">
                <hr width="80%" />
                <a href="{umbraco.library:NiceUrl(@id)}" class="youMayAlsoTitle">
                    <!-- Use their own templates for specific output --> <xsl:apply-templates select="." />
                </a>
            </xsl:if>
        </xsl:for-each>
    </xsl:template>
    
    <xsl:template match="Article">
        <xsl:value-of select="articleTitle" />
    </xsl:template>
    
    <xsl:template match="Review">
        <xsl:value-of select="reviewTitle" />
    </xsl:template>
    

    /Chriztian

  • Sarah 40 posts 104 karma points
    Oct 21, 2012 @ 01:45
    Sarah
    0

    Hi Chriztian,

    Thank you so much for your quick response. That has worked perfectly! I thought I may need to do something with the @id and break the logic down but I was struggling with my limited knowledge! I understand what you've done and I think I can probably go and clean up some of my other XSLT macros now! 

    Thanks again :)

    Sarah

Please Sign in or register to post replies

Write your reply to:

Draft