RSS and XSLT: How to display a maximum number of articles
RSS in email is a great way of spreading content. You publish your content once, load it in an email and send it to your email subscribers with minimal effort and great results. But do you really want to overload your subscribers with a long list of old content? Why not show only the latest ones by setting a maximum number of articles instead? Here's how to do that with XSLT in Copernica.
Getting started
If you already have an email document with an RSS feed, thats great! If you don't, grab your favorite RSS feed or use the one below.
http://www.zdnet.com/blog/rss.xml
Note: Need help setting up an email document with an RSS feed? You can find more information about loading a feed in the online help documentation.
Now that you have added your feed, view the document in personalized mode to see the content inside the email document. Just click on 'Preview document' to do this. You should see every article of the feed.
Getting control over the feed with XSLT
Great, so you've added the feed to your email, but we only want to show a maximum number of articles. Preferably the most recent articles. Let's say the last 5 articles. That's where XSLT comes in handy. In case you didn't know, XSLT is a language for transforming XML documents into XHTML documents or other XML documents. XSLT stands for XSL Transformations.
An RSS or Atom feed is in fact an XML document and needs to be transformed into HTML/XHTML for it to work in email. Copernica does this automatically. And by using a custom XSLT you can have more control over your RSS inside an email document. Use it to remove images for example, or to just show the title of an article. Or in this case show only the 5 most recent ones.
Using XSLT for RSS or Atom feeds
When you create an XSLT document in Copernica, you can choose to fill the document with example codes for two types of feeds: RSS or Atom. Below you'll find an XSLT document for RSS feeds. You can skip to the part of Atom feeds if you like.
This is how an XSLT for RSS can look like:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="rss/channel">
<xsl:call-template name="item" />
</xsl:template>
<xsl:template name="item">
<xsl:for-each select="item">
<div>
<a href="{link}"><xsl:value-of select="title" disable-output-escaping="yes" /></a>
<xsl:value-of select="description" disable-output-escaping="yes" />
</div>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
What this code does, is loop through the list of articles of the RSS feed and outputting the content in XHTML. Hence the 'loop code: <xsl:for-each select="item">
.
<xsl:for-each select="item">
<div>
<a href="{link}"><xsl:value-of select="title" disable-output-escaping="yes" /></a>
<xsl:value-of select="description" disable-output-escaping="yes" />
</div>
</xsl:for-each>
All we need to do is loop through the list of articles and only grab the last 5 articles by changing one line of code from:
<xsl:for-each select="item">
to:
<xsl:for-each select="//*[local-name()='item'][position() < 6]">
Note: The [position() < 6] part is where the magic happens. The XSLT only selects the last 5 articles because of < 6 which literally means 'The final XSLT code should look like this:
<xsl:for-each select="//*[local-name()='item'][position() < 6]">
<div>
<a href="{link}"><xsl:value-of select="title" disable-output-escaping="yes" /></a>
<xsl:value-of select="description" disable-output-escaping="yes" />
</div>
</xsl:for-each>
What if I have an Atom feed?
Not to worry. You can also create an XSLT document for Atom feeds in Copernica. The syntax is a bit different, but in fact you only need to change. So after creating an XSLT document for Atom feeds, find the for-each
that loops through atom:entry
.
Change it from:
<xsl:for-each select="atom:entry">
to:
<xsl:for-each select="atom:entry[position() < 6]">
That's it!
Your email document should now only show the last 5 articles of the feed, whether you're using an RSS or Atom feed. If you have anything to add, or need any help, please let me know in the comments section.