Copied to clipboard

Flag this post as spam?

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


  • Luke Curtis 5 posts 25 karma points
    May 05, 2012 @ 18:03
    Luke Curtis
    0

    List of two containing two different document types

    Hi there,

    I'm trying to make a list that contains news items and new gallaries - ordered by the date they were created. I've managed to loop through the two seperatly and call the approprate templates, but i'm struggling to get them both in the same ordered list.

    This is the for-each loop for the news:

      <xsl:for-each select="$currentPage/ancestor-or-self::umbBlog//umbBlogPost">  

     

    and this is for the gallery album:

      <xsl:for-each select="$currentPage//umbGalleryAlbum">

    I've been trying to do something like this:

      <xsl:for-each select="$currentPage/*[ancestor-or-self::umbBlog//umbBlogPost|//umbGalleryAlbum]">  

     

    But I can't get it to work. 

    I've also trying to loop through everything and just filter on the alais, but I cant get that to work either. Can anyone suggest a way to do this? Many thanks.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 05, 2012 @ 18:16
    Chriztian Steinmeier
    1

    Hi Luke,

    Here's how I'd usually approach this - should be easy to modify:

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:umb="urn:umbraco.library"
        exclude-result-prefixes="umb"
    >
    
        <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
    
        <xsl:param name="currentPage" />
        <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level = 1]" />
    
        <!-- Find all the News items -->
        <xsl:variable name="newsItems" select="$siteRoot//umbBlogPost" />
    
        <!-- Find galleries below currentPage -->
        <xsl:variable name="galleryItems" select="$currentPage//umbGalleryAlbum" />
    
        <xsl:template match="/">
            <!-- Go through them all sorted by created date -->
            <xsl:apply-templates select="$newsItems | $galleryItems">
                <xsl:sort select="@createDate" data-type="text" order="descending" />
            </xsl:apply-templates>
        </xsl:template>
    
        <!-- Template for News -->
        <xsl:template match="umbBlogPost">
            <div class="news">
                <xsl:value-of select="@nodeName" />
            </div>
        </xsl:template>
    
        <!-- Template for a Gallery -->
        <xsl:template match="umbGalleryAlbum">
            <div class="gallery">
                <xsl:value-of select="@nodeName" />
            </div>
        </xsl:template>
    
    </xsl:stylesheet>

    /Chriztian

  • Luke Curtis 5 posts 25 karma points
    May 05, 2012 @ 19:08
    Luke Curtis
    0

    Wow, that's fantasitc thanks Chriztian! I shall try this and post how I get on. Thanks

  • Luke Curtis 5 posts 25 karma points
    May 05, 2012 @ 19:43
    Luke Curtis
    0

    Wow, that's fantasitc thanks Chriztian it works perfectly first time! Thanks, been trying to crack this all day!

  • Luke Curtis 5 posts 25 karma points
    May 06, 2012 @ 16:34
    Luke Curtis
    0

    I'm having a bit of trouble working out how to get this to just show the top X number of blogs/gallery albums now that i'm using <xsl:apply-templates rather than <xsl:for-each . Im trying to do something like this:

        <xsl:template match="/">
              <xsl:if test="position() &lt; $numberOfPosts">
                    <!-- Go through them all sorted by created date -->
                    <xsl:apply-templates select="$newsItems | $galleryItems">
                            <xsl:sort select="@postDate" data-type="text" order="descending" />
                    </xsl:apply-templates>
              </xsl:if>
        </xsl:template>
     

    but position() doesnt seem to have a value. Any ideas? Many thanks.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    May 06, 2012 @ 16:53
    Chriztian Steinmeier
    0

    Hi Luke,

    The trick is to know that position() always refers to the "current node's position within the current node-set" - when using apply-templates, the current node-set is all the nodes you select in the apply-templates's select attribute (same as when you use for-each). This means that every template that apply-templates instantiates will have its own unique position() within the combined set - so you can perform the test within the individual templates to achieve the same result, e.g.:

    <xsl:template match="umbBlogPost">
       <xsl:if test="position() &lt;= $numberOfPosts">
          ...
       </xsl:if>
    </xsl:template>

    /Chriztian 

  • Luke Curtis 5 posts 25 karma points
    May 06, 2012 @ 17:15
    Luke Curtis
    0

    Ah. I see. And it works like a charm. Thanks again!

Please Sign in or register to post replies

Write your reply to:

Draft