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 241 posts 372 karma points
    Aug 23, 2011 @ 10:25
    Frederik T
    0

    Limiting a list of document types to a specific number

    Ok, this is either tricky or stupidly simple.

    It started in this thread: http://our.umbraco.org/forum/developers/xslt/23108-List-of-all-documents-from-all-descendant-pages

    Which i got help with and the code worked perfectly. The biggest, and only problem with it now is limiting the number of nodes shown. Problem is, i cant get it to work properly. The thing is i dont know how xsl:templates work so i dont know where and how to limit the number of items. I once got it working, but it looked like it limited the number of document types and not the number of listed items.

    For good measure here is the code again:

    <xsl:template match="/">
      <ul class="list">
        <!-- Process ALL descendants of $currentPage -->
        <xsl:apply-templates select="$currentPage/descendant::*[@isDoc][not(umbracoNaviHide = 1)]">
          <xsl:sort select="articleDate[normalize-space()]" order="descending" />
          <xsl:sort select="@updateDate[not(normalize-space(../articleDate))]" order="descending" />
        </xsl:apply-templates>
      </ul>
    </xsl:template>

    <!-- Template for specific DocumentTypes -->
    <xsl:template match="ArtikelStudier | ArtikelStudiejob | Eksamen | ArtikelLegatsoegning | ArtikelUdIVerden | ArtikelOekonomi | ArtikelVelvre | ArtikelTop5">
      <li>
        <a href="{umbraco.library:NiceUrl(@id)}">
          <!-- Take first image (if any) -->
          <xsl:apply-templates select="outputPictures[1]" />
          <!-- Article heading and teaser text-->
          <h5><xsl:value-of select="articleHeading"/></h5>
          <p class="teaser"><xsl:value-of select="umbraco.library:TruncateString(artikel-Teasertekst,70,'...')"/></p>
        </a>
      </li>
    </xsl:template>

    <!-- Template for thumbnail image -->
    <xsl:template match="outputPictures">
      <img src="/ImageGen.ashx?image={DAMP[@fullMedia]/mediaItem/Image/umbracoFile}&amp;width=125&amp;height=70" width="125" height="70" />
    </xsl:template>

    <!-- Template for all non-matched DocumentTypes -->
    <xsl:template match="*[@isDoc]" priority="-1">
            <!-- Suppress output... -->
    </xsl:template>

    </xsl:stylesheet>

    That is the code right now, and this is how i think it would work:

    <xsl:template match="/">
    <ul class="list">
    <!-- Process ALL descendants of $currentPage -->
    <xsl:if test="position() &lt;= 5">
    <xsl:apply-templates select="$currentPage/descendant::*[@isDoc][not(umbracoNaviHide = 1)]">
    <xsl:sort select="articleDate[normalize-space()]" order="descending" />
    <xsl:sort select="@updateDate[not(normalize-space(../articleDate))]" order="descending" />
    </xsl:apply-templates>
    </xsl:if>
    </ul>
    </xsl:template>

    But that obviously doesnt work with where the if statement is.

     

    So, hopefully you guys understand my predicament, and hopefully the solution is simple enough. Thank you for your time.

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Aug 23, 2011 @ 10:43
    Chriztian Steinmeier
    0

    Hi Frederik,

    This is one of the edge cases where you have to use the for-each instruction instead of apply-templates - but you can keep your templates if you do it like this:

     <xsl:template match="/">
        <ul class="list">
            <xsl:for-each select="$currentPage/descendant::*[@isDoc][not(umbracoNaviHide = 1)]">
                <xsl:sort select="articleDate[normalize-space()]" order="descending" />
                <xsl:sort select="@updateDate[not(normalize-space(../articleDate))]" order="descending" />
    
                <xsl:if test="position() &lt;= 5">
                    <xsl:apply-templates select="." />
                </xsl:if>
            </xsl:for-each>
        </ul>
    </xsl:template>
    /Chriztian
  • Frederik T 241 posts 372 karma points
    Aug 23, 2011 @ 10:49
    Frederik T
    0

    Perfect! Thank you for the quick reply and thank you for the solution! There was a tiny typing mistake in your code but that was an easy fix ;)

    Here is the fixed and final snippet for future use if anyone has the same problem:

    <xsl:template match="/">
    <ul class="list">
    <xsl:for-each select="$currentPage/descendant::*[@isDoc][not(umbracoNaviHide = 1)]">
    <xsl:sort select="articleDate[normalize-space()]" order="descending" />
    <xsl:sort select="@updateDate[not(normalize-space(../articleDate))]" order="descending" />
    <xsl:if test="position() &lt;= 5">
    <xsl:apply-templates select="." />
    </xsl:if>
    </xsl:for-each>
    </ul>
    </xsl:template>
  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 8x admin c-trib
    Aug 23, 2011 @ 11:02
    Chriztian Steinmeier
    0

    Ah yes - I edited that 4 times because the forum insisted on keeping it all on one line :-)

    (I've corrected it now, so it looks like I'm flawless :-)

    /Chriztian

  • Frederik T 241 posts 372 karma points
    Aug 23, 2011 @ 11:20
    Frederik T
    0

    ah ok, yeah the forum only works when it wants to :p

Please Sign in or register to post replies

Write your reply to:

Draft