Copied to clipboard

Flag this post as spam?

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


  • Steve 472 posts 1216 karma points
    Nov 05, 2012 @ 17:01
    Steve
    0

    Sorting News Articles by Publication Date

    Hello,

    I have got some xslt that pulls "featuredImages" property from the "NewsArticle" doctype and puts them in a slideshow. I am having some problems getting them to sort by the property "publicationDate" as I keep ending up with duplicate niceURL's for the "NewsArticle"'s. I am sure it's the placment of my <xsl:for-each>, but I can't find the right location for it. Any help would be apppreciated. Thanks.

    <xsl:param name="currentPage"/>
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level=1]"/>
    <xsl:template match="/">
     
      <xsl:variable name="articleNode" select="$siteRoot/NewsHome/NewsCategory[3]/DateFolder" />
      <xsl:if test="count($articleNode/NewsArticle[featureThisArticle = 1]) &gt; 0">
       
        <div class="block-gallery">
         <ul class="galery">
            <xsl:apply-templates select="$articleNode" />
          </ul>
          <div class="slider">
            <class="prev" href="#">prev</a>
            <div class="switcher">&nbsp;</div>
            <class="next" href="#">next</a>
          </div>
        </div>
        </xsl:if>
    </xsl:template>
            
            
    <xsl:template match="NewsArticle">
      <xsl:variable name="sortedArticles" select="$siteRoot/NewsHome/NewsCategory[3]/DateFolder/NewsArticle[featureThisArticle = 1]" />
     <xsl:for-each select="$sortedArticles"> 
    <xsl:sort select="publicationDate" order="descending" />
      <xsl:if test="normalize-space(featuredImage)">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(featuredImage, 0)/umbracoFile"/>
       
        <li>
          <div class="fade">&nbsp;</div>
          <img src="/ImageGen.ashx?image={umbraco.library:GetMedia(featuredImage, 'false')/umbracoFile}&amp;height=325" />
          
          <div class="block-galery">
           <img class="grey-seal" src="/media/831667/grey-seal.png"/>
            <div class="text-area">
            <div class="text">
              <strong><xsl:value-of select="headline" /></strong>
              <span><xsl:value-of select="teaser" /></span>
              <xsl:if test="$currentPage/ancestor-or-self::*/@id > 0">
                 <class="read-story" href="{umbraco.library:NiceUrl(./@id)}">FULL STORY</a>

              </xsl:if>
              
            </div>
      
          </div>
          </div>
        </li>
      </xsl:if>
     </xsl:for-each>
    </xsl:template>
       
    </xsl:stylesheet>

  • Steve 472 posts 1216 karma points
    Nov 05, 2012 @ 20:37
    Steve
    0

    Why doesn't this approach work? I am sorting in the middle of the "apply-templates".

     

    <xsl:param name="currentPage"/>
    <xsl:variable name="siteRoot" select="$currentPage/ancestor-or-self::*[@level=1]"/>
    <xsl:template match="/">
     
      <xsl:variable name="articleNode" select="$siteRoot/NewsHome/NewsCategory[3]/DateFolder" />
      <xsl:if test="count($articleNode/NewsArticle[featureThisArticle = 1]) &gt; 0">
       
        <div class="block-gallery">
         <ul class="galery">

            <xsl:apply-templates select="$articleNode" />
    <xsl:sort select="publicationDate" order="descending" />

           </xsl:apply-templates>
          </ul>
          <div class="slider">
            <class="prev" href="#">prev</a>
            <div class="switcher">&nbsp;</div>
            <class="next" href="#">next</a>
          </div>
        </div>
        </xsl:if>
    </xsl:template>

    <xsl:template match="NewsArticle">
      
      <xsl:if test="normalize-space(featuredImage)">
        <xsl:variable name="mediaNode" select="umbraco.library:GetMedia(featuredImage, 0)/umbracoFile"/>
       
        <li>
          <div class="fade">&nbsp;</div>
          <img src="/ImageGen.ashx?image={umbraco.library:GetMedia(featuredImage, 'false')/umbracoFile}&amp;height=325" />
          
          <div class="block-galery">
           <img class="grey-seal" src="/media/831667/grey-seal.png"/>
            <div class="text-area">
            <div class="text">
              <strong><xsl:value-of select="headline" /></strong>
              <span><xsl:value-of select="teaser" /></span>
              <xsl:if test="$currentPage/ancestor-or-self::*/@id > 0">
                 <class="read-story" href="{umbraco.library:NiceUrl(./@id)}">FULL STORY</a>

              </xsl:if>
              
            </div>
      
          </div>
          </div>
        </li>
      </xsl:if>
     </xsl:for-each>
    </xsl:template>
       
    </xsl:stylesheet>

  • Steve 472 posts 1216 karma points
    Nov 05, 2012 @ 21:31
    Steve
    0

    got it working with:

    <xsl:apply-templates select="$articleNode/NewsArticle">
              <xsl:sort select="publicationDate" order="descending" />
           </xsl:apply-templates>

    Any suggestions on keeping the number of NewsArticles to 12? Even if there are more in the Folder containing them.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Nov 05, 2012 @ 22:55
    Chriztian Steinmeier
    100

    Hi Steve,

    Great to see you got it "sorted" :-)

    To limit the number of articles output, just substitute a for-each for the apply-templates and test the position within:

    <xsl:for-each select="$articleNode/NewsArticle">
        <xsl:sort select="publicationDate" order="descending" />
        <xsl:if test="position() &lt;= 12">
            <xsl:apply-templates select="." />
        </xsl:if>
    </xsl:for-each>

    /Chriztian

  • Steve 472 posts 1216 karma points
    Nov 07, 2012 @ 15:14
    Steve
    0

    Thanks Chriztian! That did the trick. I don't know why I didn't see that. As always, you are a big help.

Please Sign in or register to post replies

Write your reply to:

Draft