limiting amount of articles dispalyed from RSS feed using XSLT

matthew.wren's Avatar

matthew.wren

14 Jan, 2015 04:41 PM

We have a blog feed on our history page in the right column and it's using an RSS block and an XSLT format block. I'm wondering if there's a way to display only 5 of these blog posts by editing the xlst format or if it can only be done from the feed itself.

Here's the link for the feed:

http://blogs.scranton.edu/history/feed/

Here's the format for it:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" xmlns:dc="http://www.oreillynet.com/meerkat/dc" xmlns:rdf="http://www.oreillynet.com/meerkat/" xmlns:rss="http://purl.org/rss/1.0/" xmlns:sy="http://www.oreillynet.com/meerkat/sy" xmlns:wfw="http://wellformedweb.org/CommentAPI/">
    <xsl:output indent="yes" method="xml"/>
    <xsl:template match="/">
            <!-- <xsl:apply-templates/> -->
            <h1>From the Blog</h1>
            <div class="FeedData">
            <xsl:apply-templates select="rss/channel/item"/>
            <hr/>
            <p><xsl:text>From the </xsl:text><a href="http://blogs.scranton.edu/history/">History Dept. Blog</a></p>
        </div>
    </xsl:template>
    <xsl:template match="system-data-structure/block-content/content">
            <!-- For RSS -->
            <xsl:apply-templates select="rss/channel"/>
            <xsl:apply-templates/>
    </xsl:template>
    <!-- *** START RSS *** -->
    <xsl:template match="rss/channel">
        <h4>
                <xsl:value-of select="title"/>
        </h4>
        <p> 
            <xsl:value-of select="description"/>
        </p>
        <xsl:apply-templates select="item"/>
    </xsl:template>
    <xsl:template match="rss/channel/item">
        <xsl:variable name="paragraph" select="description"/>
            <h4>
              <a>
                <xsl:attribute name="href">
                <xsl:value-of select="link"/>
                </xsl:attribute>
             <xsl:value-of select="title"/>
                </a>
            </h4>
            <p>
            <xsl:call-template name="doPseudoHTMLReplace">
                    <xsl:with-param name="current-text">
            <xsl:value-of select="substring-before(substring-after($paragraph, '&gt;'),'.&lt;')"/>
                </xsl:with-param>
            </xsl:call-template>
            </p>
            <!-- 
                <xsl:value-of select="substring-before(substring-after($paragraph, '&gt;'),'&lt;')"/>
                -->
                <!-- <xsl:value-of select="substring($paragraph, 4, 104)" /><xsl:text>...</xsl:text> -->
                <!--
                <xsl:call-template name="doPseudoHTMLReplace">
                    <xsl:with-param name="current-text">
                        <xsl:value-of select="substring($paragraph, 4, 104)" /><xsl:text>...</xsl:text>
                    </xsl:with-param>
                </xsl:call-template><xsl:text>...</xsl:text>
                -->
                <!--
                <xsl:for-each select="str:tokenize(string(description))">
                    <xsl:if test="position() &lt; 50">
                    <xsl:value-of select="."/>
                    <xsl:text> </xsl:text>
                    </xsl:if>
                </xsl:for-each>
                -->
    </xsl:template>
    <!-- *** END RSS *** -->
<!--
    |
    | Rewrite any pseudo HTML in the content
    |
    -->
    <!--
        <xsl:call-template name="doPseudoHTMLReplace">
          <xsl:with-param name="current-text">
             <xsl:copy-of select="text"/>
          </xsl:with-param>
        </xsl:call-template>
    -->
    <xsl:template match="text()">
      <xsl:call-template name="doPseudoHTMLReplace">
         <xsl:with-param name="current-text">
            <xsl:value-of select="."/>
         </xsl:with-param>
      </xsl:call-template>
   </xsl:template>
   <xsl:template name="doPseudoHTMLReplace">
      <xsl:param name="current-text"/>
      <xsl:if test="string($current-text)">
         <xsl:choose>
            <xsl:when test="contains($current-text, '&lt;b&gt;')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'&lt;b&gt;')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <xsl:variable name="formatted-text">
                  <xsl:value-of select="substring-before(substring-after($current-text,'&lt;b&gt;'),'&lt;/b&gt;')"/>
               </xsl:variable>
               <strong>
                  <xsl:call-template name="doPseudoHTMLReplace">
                     <xsl:with-param name="current-text">
                        <xsl:value-of select="$formatted-text"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </strong>
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-after($current-text,'&lt;/b&gt;')"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($current-text, '&lt;p&gt;')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'&lt;p&gt;')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <xsl:variable name="formatted-text">
                  <xsl:value-of select="substring-before(substring-after($current-text,'&lt;p&gt;'),'&lt;/p&gt;')"/>
               </xsl:variable>
                  <xsl:call-template name="doPseudoHTMLReplace">
                     <xsl:with-param name="current-text">
                        <xsl:value-of select="$formatted-text"/>
                     </xsl:with-param>
                  </xsl:call-template>
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-after($current-text,'&lt;/p&gt;')"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($current-text, '&lt;i&gt;')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'&lt;i&gt;')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <xsl:variable name="formatted-text">
                  <xsl:value-of select="substring-before(substring-after($current-text,'&lt;i&gt;'),'&lt;/i&gt;')"/>
               </xsl:variable>
               <em>
                  <xsl:call-template name="doPseudoHTMLReplace">
                     <xsl:with-param name="current-text">
                        <xsl:value-of select="$formatted-text"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </em>
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-after($current-text,'&lt;/i&gt;')"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($current-text,'[link]')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'[link]')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <xsl:variable name="link-text">
                  <xsl:value-of select="substring-before(substring-after($current-text,'[link]'),'[/link]')"/>
               </xsl:variable>
               <xsl:variable name="display-text">
                  <xsl:if test="contains($link-text,'[text]')">
                     <xsl:value-of select="substring-before(substring-after($link-text,'[text]'),'[/text]')"/>
                  </xsl:if>
               </xsl:variable>
               <xsl:variable name="href"><xsl:value-of select="substring-before(substring-after($link-text,'[href]'),'[/href]')"/></xsl:variable>
               <a href="{$href}">
                  <xsl:if test="starts-with($href,'http')">
                     <xsl:attribute name="target">_blank</xsl:attribute>
                  </xsl:if>
                  <xsl:choose>
                     <xsl:when test="string($display-text)">
                        <xsl:call-template name="doPseudoHTMLReplace">
                           <xsl:with-param name="current-text">
                              <xsl:value-of select="$display-text"/>
                           </xsl:with-param>
                        </xsl:call-template>
                     </xsl:when>
                     <xsl:otherwise>
                        <xsl:value-of select="$href"/>
                     </xsl:otherwise>
                  </xsl:choose>
               </a>
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-after($current-text,'[/link]')"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($current-text, '[break/]')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'[break/]')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <br/>
               <br/>
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-after($current-text,'[break/]')"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($current-text,'http://')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'http://')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <xsl:call-template name="doPrefixPseudoHTMLReplace">
                  <xsl:with-param name="prefix">http://</xsl:with-param>
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="$current-text"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:when test="contains($current-text,'https://')">
               <xsl:call-template name="doPseudoHTMLReplace">
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="substring-before($current-text,'https://')"/>
                  </xsl:with-param>
               </xsl:call-template>
               <xsl:call-template name="doPrefixPseudoHTMLReplace">
                  <xsl:with-param name="prefix">https://</xsl:with-param>
                  <xsl:with-param name="current-text">
                     <xsl:value-of select="$current-text"/>
                  </xsl:with-param>
               </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
               <xsl:copy-of select="$current-text"/>
            </xsl:otherwise>
         </xsl:choose>
      </xsl:if>
   </xsl:template>
   <xsl:template name="doPrefixPseudoHTMLReplace">
      <xsl:param name="prefix"/>
      <xsl:param name="current-text"/>
      <xsl:if test="string($prefix) and string($current-text)">
         <xsl:variable name="text-after">
            <xsl:value-of select="substring-after($current-text,$prefix)"/>
         </xsl:variable>
         <xsl:variable name="link">
            <xsl:choose>
               <xsl:when test="string(substring-before($text-after,' ')) or string(substring-before($text-after,')'))">
                  <xsl:call-template name="getShorterString">
                     <xsl:with-param name="string1">
                        <xsl:value-of select="substring-before($text-after,' ')"/>
                     </xsl:with-param>
                     <xsl:with-param name="string2">
                        <xsl:value-of select="substring-before($text-after,')')"/>
                     </xsl:with-param>
                  </xsl:call-template>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:value-of select="$text-after"/>
               </xsl:otherwise>
            </xsl:choose>
         </xsl:variable>
         <a href="{$prefix}{$link}" target="_blank">
            <xsl:value-of select="$prefix"/>
            <xsl:value-of select="$link"/>
         </a>
         <xsl:call-template name="doPseudoHTMLReplace">
            <xsl:with-param name="current-text">
               <xsl:value-of select="substring-after($text-after,$link)"/>
            </xsl:with-param>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>
   <xsl:template name="getShorterString">
      <xsl:param name="string1"/>
      <xsl:param name="string2"/>
      <xsl:choose>
         <xsl:when test="not(string($string1))">
            <xsl:value-of select="$string2"/>
         </xsl:when>
         <xsl:when test="not(string($string2))">
            <xsl:value-of select="$string1"/>
         </xsl:when>
         <xsl:when test="string($string1) and string($string2)">
            <xsl:choose>
               <xsl:when test="string-length($string1) &gt; string-length($string2)">
                  <xsl:value-of select="$string2"/>
               </xsl:when>
               <xsl:otherwise>
                  <xsl:value-of select="$string1"/>
               </xsl:otherwise>
            </xsl:choose>
         </xsl:when>
      </xsl:choose>
   </xsl:template>
</xsl:stylesheet>
  1. 1 Posted by Ryan Griffith on 16 Jan, 2015 06:42 PM

    Ryan Griffith's Avatar

    Hi Matthew,

    If the items within the feed are already sorted by date, you can simply replace the following line:

    <xsl:apply-templates select="item"/>
    

    With:

    <xsl:apply-templates select="item[position() &lt;= 5]"/>
    

    If the items are not sorted, you will have issues because the pubDate is not a string that is easily sortable. I found this Stackoverflow article that provides a great solution to this. Once you are able to sort the items, you would do something like the following within the item template:

    <xsl:template match="rss/channel/item">
        <xsl:if test="position() &lt;= 5">
            <!-- code to process the item goes here... -->
        </xsl:if>
    </xsl:template>
    

    Please let me know if you have any questions.

    Thanks!

  2. Ryan Griffith closed this discussion on 16 Feb, 2015 08:51 PM.

Discussions are closed to public comments.
If you need help with Cascade CMS please start a new discussion.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac

 

26 Aug, 2016 01:19 PM
25 Aug, 2016 03:02 PM
25 Aug, 2016 12:50 PM
24 Aug, 2016 08:43 PM
24 Aug, 2016 07:20 PM
21 Aug, 2016 01:20 PM