Copied to clipboard

Flag this post as spam?

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


  • Eddie Foreman 215 posts 288 karma points
    Sep 16, 2014 @ 18:33
    Eddie Foreman
    0

    Sort by date with getEverythingWithTags

    Hi All,

    Part of my xslt grabs all of the items with a certain tag.

    <xsl:variable name="matchedNodes" select="Exslt.Tags:getEverythingWithTags($tags)/root/node" />
    <xsl:for-each select="$matchedNodes">
    <xsl:sort select="publishedDate" order="descending"  />
    <xsl:for-each select="umbraco.library:GetXmlNodeById(@id)">
    <p><xsl:value-of select="umbraco.library:FormatDateTime(publishedDate,'dd MMMM yyyy')" /></p>
    </xsl:for-each>
    </xsl:for-each>

    I've tried to sort them using the normal sort method, but as matchedNodes is a list of ids there is no date to sort.

    Does anyone have an example of sorting with 'getEverythingWithTags'? 

    Thanks,
    Eddie

     

  • Chriztian Steinmeier 2798 posts 8788 karma points MVP 7x admin c-trib
    Sep 16, 2014 @ 19:37
    Chriztian Steinmeier
    100

    Hi Eddie,

    You should be able to do something like this - the trick is of course to use the actual referenced nodes, which is what we're grabbing with the id() function - it accepts a nodeset (all the @id attributes of the $matchedNodes).

    <xsl:template match="/">
        <xsl:variable name="matchedNodes" select="Exslt.Tags:getEverythingWithTags($tags)/root/node" /> 
        <!--
            Loops only a single node, just to get into the right context.
            (See: https://gist.github.com/greystate/8576730)
        -->
        <xsl:for-each select="$currentPage">
            <xsl:apply-templates select="id($matchedNodes/@id)">
                <xsl:sort select="publishedDate" data-type="text" order="descending" />
            </xsl:apply-templates>
        </xsl:for-each> 
    </xsl:template>
    
    <!-- Output template -->
    <xsl:template match="*[@isDoc]">
        <p>
            <xsl:value-of select="umbraco.library:FormatDateTime(publishedDate, 'dd MMMM yyyy')" />
        </p>
    </xsl:template>
    

    Let me know if it works - maybe it needs some tuning, coming right out of my head without testing :-)

    /Chriztian

  • Eddie Foreman 215 posts 288 karma points
    Sep 17, 2014 @ 10:52
    Eddie Foreman
    0

    Hi Chriztian,

    Thanks for the example. The existing xslt has a number of templates used for paging, output etc..

    Below is the part of the code that grabs the tags.

    <xsl:variable name="matchedNodes" select="Exslt.Tags:getEverythingWithTags($tags)/root/node" />
        <xsl:for-each select="$matchedNodes">                        
            <xsl:for-each select="umbraco.library:GetXmlNodeById(@id)">
            <xsl:call-template name="article" />
            </xsl:for-each>
        </xsl:for-each>
    

    Will see if I can get it working with call-template. Just wanted to check if you knew if it's possible with the call-template approach or if it's only possible with apply-templates.

    Thanks, Eddie

  • Eddie Foreman 215 posts 288 karma points
    Sep 17, 2014 @ 15:12
    Eddie Foreman
    0

    Hi Chriztian,

    All working now. Used your idea to create a new variable that contained the ids in descending date order. Then tweaked my existing could to use the new variable and display the item.

    <!-- Get all nodes that match the tag -->   
                  <xsl:variable name="matchedNodes" select="Exslt.Tags:getEverythingWithTags($tags)/root/node" />
    
                  <!-- Sort matched nodes into date order-->    
                  <xsl:variable name="sortedMacthedNodes">
                    <xsl:for-each select="$currentPage">
                      <xsl:apply-templates select="id($matchedNodes/@id)">
                        <xsl:sort select="publishedDate" data-type="text" order="descending" />
                      </xsl:apply-templates>
                    </xsl:for-each>
                  </xsl:variable>
    
                  <!-- Display each node -->    
                  <xsl:for-each select="msxsl:node-set($sortedMacthedNodes)/node">
                    <xsl:if test="position() &gt; $itemsPerPage * number($pageNumber - 1) and position() &lt;= number($itemsPerPage * number($pageNumber - 1) + $itemsPerPage)">
                      <xsl:for-each select="umbraco.library:GetXmlNodeById(@id)">
                        <xsl:call-template name="article" />
                      </xsl:for-each>
                    </xsl:if>
                  </xsl:for-each> 
    

    Thanks again for the pointers, Eddie

Please Sign in or register to post replies

Write your reply to:

Draft