Here's the scenario: We're have an xml set of nodes, returned in the same order as they appear in the umbraco panel. We can obviously use sort (whether we use appy-template or foreach), and order them by date in ASC or DESC order.

The problem begins when we need to link the nodes by adding previous (node, article, blog post...) and next links. Using node::following or node::preceding would not solve the problem as they both point to the structure initially being returned by umbraco - before they get sorted by date.

The following approach isn't perfect, as we end up replicating xml data, and with 100+ nodes would fill up the momeory, however it works pretty well and it may save someone a lot of time.

The plan is to create the vartiable that holds the nodes we need - sorted in the correct order; and simply apply-templates or use foreach to populate the data. What we can do then, is use node::following and node::preceding to get the next/previous links within the context of the variable we created instead of $currentPage.

<!-- 1. save sorted nodes into a variable -->
<xsl:variable name="sortedItems">
  <xsl:for-each select="$currentPage/../node[@nodeTypeAlias = 'XXX']">
     <xsl:sort select="CONDITION" order="descending or ascending" />
        <xsl:copy-of select="." />

<!-- 2. call template using sortedItems as a param -->
<xsl:call-template name="displayItems">
   <xsl:with-param name="nodes" select="msxml:node-set($sortedItems)" />

<!-- 3. get previous/next link -->
<xsl:template name="displayItems">
   <xsl:param name="nodes"/>
   <xsl:variable name="currentNode" select="$nodes/node[@id = $currentPage/@id]" />   
   <xsl:variable name="nextItem" select="$currentNode/preceding::node" />
   <xsl:variable name="previousItem" select="$currentNode/following::node" />

   <xsl:if test="string($previousItem)">
      <a href="{umbraco.library:NiceUrl($previousItem/@id)}">previous</a>

   <xsl:if test="string($nextItem)">
      <a href="{umbraco.library:NiceUrl($nextItem/@id)}">next</a>


Tagged: template  umbraco  web programming  xsl:following-sibling  xsl:preceding-sibling  xsl:sort  xslt