Edit online

Dynamically Generating an RSS Feed

3 Feb 2022
Read time: 5 minute(s)

When publishing a website from DITA XML content, we can dynamically generate an RSS XML feed by customizing the WebHelp Responsive output.

DITA WebHelp output can be customized using a publishing template mechanism. Inside a publishing template folder, there is an opt file that can contain links to various XSLT stylesheets that are useful for customizations. For example, we'll add a link to a stylesheet for processing such special keyword label elements:
<publishing-template>
    <name>.....</name>
    ......
        <xslt>
            ....
            <extension file="xslt/createRSS.xsl" id="com.oxygenxml.webhelp.xsl.createNavLinks"/>
            .....
        </xslt>
    </webhelp>
</publishing-template>
The createRSS.xsl stylesheet generates an rss.xml file that contains references to all topics referenced in the DITA Map:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    xmlns:toc="http://www.oxygenxml.com/ns/webhelp/toc" 
    version="2.0">
    
    <xsl:param name="JSON_OUTPUT_DIR_URI"/>
    <xsl:template match="/toc:toc">
        <xsl:next-match/>
        <xsl:apply-templates mode="rss" select="."/>
    </xsl:template>
    
    <xsl:template match="/toc:toc" mode="rss">
        <xsl:result-document href="{concat($JSON_OUTPUT_DIR_URI, '/../../../../rss.xml')}" indent="yes">
            <rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
                <channel>
                    <title>TITLE OF RSS FEED HERE</title>
                    <link>/rss.xml</link>
                    <description>DESCRIPTION OF FEED HERE</description>
                    <language>en-us</language>
                    <atom:link href="https://my.website/rss.xml" rel="self" type="application/rss+xml" xmlns:atom="http://www.w3.org/2005/Atom"/>
                    <lastBuildDate><xsl:value-of select="format-dateTime(current-dateTime(),'[F], [D] [M01] [Y] [h] [P] [z]')"/></lastBuildDate>
                    <docs>https://oxygenxmlblog.netlify.com/</docs>
                    <generator>FEED GENERATOR NAME</generator>
                    <managingEditor>EDITOR EMAIL INFORMATION</managingEditor>
                    <webMaster>WEBMASTER EMAIL INFORMATION</webMaster>
                    <copyright>Copyright Info Here. All rights reserved.</copyright>
                    <category>News</category>
                    <ttl>30</ttl>        
                    <image>
                        <url>http://my.website/img/rss-image.gif</url>
                        <title>BLOG TITLE HERE
                        <link>/rss.xml</link>
                    </image>
                    <xsl:for-each select="//toc:topic">
                        <xsl:if test="not(@href = 'javascript:void(0)')">
                            <item>
                                <title><xsl:value-of select="toc:title"/></title>
                                <link><xsl:value-of select="concat('/', @href)"/></link>   
                                <guid isPermaLink="false"><xsl:value-of select="@href"/></guid>
                                <xsl:variable name="ref" select="replace(resolve-uri(@href, base-uri()), '\.html', '.dita')"/>
                                <xsl:variable name="date" select="document($ref)/*/prolog/critdates/created/@date"/>
                                <xsl:choose>
                                    <xsl:when test="$date">
                                        <pubDate><xsl:value-of select="
                                            format-date(xs:date($date),
                                            '[F], [D01] [MNn,*-3] [Y] 00:00:00 GMT')"/></pubDate>
                                        <!-- Format like: Thu, 20 Dec 2022 02:46:11 UTC -->
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <!-- Set some fixed date so that the topic does not appear as new -->
                                        <pubDate> Wed, 1 Jan 2020 02:46:11 GMT</pubDate>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </item>
                        </xsl:if>
                    </xsl:for-each>
                </channel>
            </rss>
        </xsl:result-document>
    </xsl:template>
</xsl:stylesheet>
The published output will produce an RSS feed similar to the one here: https://blog.oxygenxml.com/rss.xml. For each topic referenced in the feed, a publication date is computed based on its creation date specified in the prolog:
<topic id="rss_feed_generation">
    <title>...</title>
    <prolog>
        <author>...</author>
        <critdates>
            <created date="YYYY-MM-DD"/>
        </critdates>

The WebHelp publishing template used for publishing this blog already has a customization to create the RSS feed: https://github.com/oxygenxml/blog/tree/master/publishing/webhelpBlogTemplate.

Using the RSS Feed With Follow.it to Receive New Posts by Email

Websites such as Follow.it allow you to register a link to your RSS feed and produce a small HTML form that can be embedded in your blog. Users who register to receive notifications when new posts are available in the RSS feed will receive automatic emails.

Once the RSS feed is available on the website, the following steps can be followed to integrate with Follow.it:
  1. Go to the Follow.it website and add a reference to your RSS feed.
  2. On the website, customize the form that will be embedded in your blog HTML contents.
  3. In the the opt file (inside the Oxygen WebHelp publishing template folder), add references to an XML file that contains the structure for it to appear after the content of each article:
    <fragment file="html-fragments/subscribe.xml" placeholder="webhelp.fragment.after.toc_or_tiles"/>
    <fragment file="html-fragments/subscribe.xml" placeholder="webhelp.fragment.after.feedback"/>
  4. Create the subscribe.xml file and paste the form HTML content created by Follow.it inside it. Make the HTML content well-formed.