Copied to clipboard

Flag this post as spam?

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


  • Bex 444 posts 555 karma points
    Feb 08, 2011 @ 12:20
    Bex
    0

    Generating RSS from Base Service

    Hi!

     

    I'm wondering if someone can tell me if I am going about this the wrong way..

    I need to generate an rss feed (which I have never done before) and I thought that using a base service would be a good way of doing this as the sites who will access the feed need to pass in parameters so we send them the right stuff and that would look better than sticking a query string in.

    I have previously returned xml similar to as follows:

     public static XPathNodeIterator GetRss(Guid param1, Guid param2)
    { //Example of generating xml XmlDocument doc = new XmlDocument(); XmlElement root = doc.CreateElement("Events"); doc.AppendChild(root); foreach (Event in events) { ~~~~ make even node } return doc.CreateNavigator().Select("//Events");
    }

    I know this doesn't quite generate the RSS properly because when I started doing it I realised that my xml must have 

    <?xml version="1.0" encoding="UTF-8"?>
    <rss version="2.0">

    at the top, but when I return xml usually I just tell it to select the top node (which in the above case will be events).

    How can I make it return the above two lines at the top? Should I be returning something different from xn XPathNodeIterator? or doing it completely differently?

    Can anyone give me an example?

    Thanx

     

    Bex

  • Tim 1193 posts 2675 karma points MVP 4x c-trib
    Feb 09, 2011 @ 01:02
    Tim
    0

    Hi Bex,

    I normally do RSS feeds as a page that just spits out the RSS using an XSLT macro. So I might create a page called "RSS" which just contains the rss output. Here's a very basic example of the macro:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE xsl:stylesheet [ <!ENTITY nbsp "&#x00A0;"> ]>
    <xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:msxml="urn:schemas-microsoft-com:xslt"
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:vb="urn:the-xml-files:xslt-vb"
      xmlns:umbraco.library="urn:umbraco.library" xmlns:Exslt.ExsltCommon="urn:Exslt.ExsltCommon" xmlns:Exslt.ExsltDatesAndTimes="urn:Exslt.ExsltDatesAndTimes" xmlns:Exslt.ExsltMath="urn:Exslt.ExsltMath" xmlns:Exslt.ExsltRegularExpressions="urn:Exslt.ExsltRegularExpressions" xmlns:Exslt.ExsltStrings="urn:Exslt.ExsltStrings" xmlns:Exslt.ExsltSets="urn:Exslt.ExsltSets" xmlns:CWS.Twitter="urn:CWS.Twitter" xmlns:umbraco.contour="urn:umbraco.contour" xmlns:ucomponents.cms="urn:ucomponents.cms" xmlns:ucomponents.dates="urn:ucomponents.dates" xmlns:ucomponents.io="urn:ucomponents.io" xmlns:ucomponents.members="urn:ucomponents.members" xmlns:ucomponents.strings="urn:ucomponents.strings" xmlns:ucomponents.urls="urn:ucomponents.urls" xmlns:ucomponents.xml="urn:ucomponents.xml" xmlns:autofolders.library="urn:autofolders.library" xmlns:tags="urn:tags" xmlns:PS.XSLTsearch="urn:PS.XSLTsearch"
      exclude-result-prefixes="msxml umbraco.library Exslt.ExsltCommon Exslt.ExsltDatesAndTimes Exslt.ExsltMath Exslt.ExsltRegularExpressions Exslt.ExsltStrings Exslt.ExsltSets CWS.Twitter umbraco.contour ucomponents.cms ucomponents.dates ucomponents.io ucomponents.members ucomponents.strings ucomponents.urls ucomponents.xml autofolders.library tags PS.XSLTsearch ">

      <xsl:output method="xml" omit-xml-declaration="yes"/>
     
      <xsl:param name="currentPage"/>
     
      <xsl:variable name="siteURL" select="concat('http://',umbraco.library:RequestServerVariables('HTTP_HOST'))"/>
        
      <xsl:template match="/">
            
        <xsl:value-of select="umbraco.library:ChangeContentType('text/xml')"/>
       
        <xsl:variable name="articles" select="$currentPage/ancestor-or-self::*[@isDoc and @level = 1]/descendant::* [@isDoc and string(umbracoNaviHide) != '1']"/>
        <xsl:variable name="pubDate">
            <xsl:for-each select="$articles/* [@isDoc and string(umbracoNaviHide) != '1']">
                <xsl:sort select="newsDate" data-type="text" order="descending" />
                <xsl:if test="position() = 1">
                      <xsl:value-of select="@createDate" />
                </xsl:if>
            </xsl:for-each>
        </xsl:variable>
           
        <xsl:text disable-output-escaping="yes">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</xsl:text>
        <rss version="2.0"
          xmlns:content="http://purl.org/rss/1.0/modules/content/"
          xmlns:wfw="http://wellformedweb.org/CommentAPI/"
          xmlns:dc="http://purl.org/dc/elements/1.1/">
          <channel>
            <title><xsl:value-of select="$currentPage/feedTitle"/></title>
            <link><xsl:value-of select="$siteURL"/><xsl:value-of select="umbraco.library:NiceUrl(1160)"/></link>
            <pubDate>
                <xsl:value-of select="$pubDate"/>
            </pubDate>
            <generator>umbraco</generator>
            <description><xsl:value-of select="$currentPage/feedDescription"/></description>
            <language>en</language>

            <xsl:for-each select="$articles">
              <xsl:sort select="@createDate" data-type="text" order="descending" />
              <xsl:if test="position() &lt;= $currentPage/numberOfItems">
                <item>
                  <title>
                    <xsl:value-of select="pageHeading"/>
                  </title>
                  <link>
                    <xsl:value-of select="$siteURL"/><xsl:value-of select="umbraco.library:NiceUrl(@id)"/>
                  </link>
                  <pubDate>
                    <xsl:value-of select="umbraco.library:FormatDateTime(@createDate, 'yyyy-MM-dd')" />
                  </pubDate>
                  <content:encoded>
                    <xsl:value-of select="concat('&lt;![CDATA[ ', ./contentSummary,']]&gt;')" disable-output-escaping="yes"/>
                  </content:encoded>
                </item>
              </xsl:if>
            </xsl:for-each>
          </channel>
        </rss>
      </xsl:template>

    </xsl:stylesheet>

    Obviously there's a bunch of stuff in there that would be different in your version, but hopefully that should give you an idea of how you can do it nice and quickly without needing to write /Base methods!

    :)

  • Bex 444 posts 555 karma points
    Feb 09, 2011 @ 09:50
    Bex
    0

    Hi Tim!

    Thanks for your reply!

    I think I worked it out! I did not realise the 

    <rss version="2.0">

    tag actually closed, I thought it was just a header tag! I managed to do it using base and am just calling the rss tag

     return doc.CreateNavigator().Select("//RSS");

     

    Think I was just being a bit daft! :)

Please Sign in or register to post replies

Write your reply to:

Draft