Copied to clipboard

Flag this post as spam?

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


  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 11:32
    Rik Helsen
    0

    best practice for retrieving external rss feed and displaying it in a template?

    I was wondering if there are any packages anywhere, or tutorials for that matter, that show how i can retrieve an external rss feed, style & customize it and present it somewhere in a template? The search function doesn't immediatly return results...

    All help is very much appreciated!

    Kind regards,
    Rik

  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 11:41
    Rik Helsen
    0
  • Fredrik 41 posts 161 karma points
    Jan 28, 2010 @ 11:49
    Fredrik
    1

    Hello Rik!

    I have some documentation on this, but it's on my private PC, If you don't make it until evening, I will dig it up when I get home :)

    Have a great day,
    Fredrik

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 28, 2010 @ 11:52
    Sebastiaan Janssen
    1

    I can recommend Darren Ferguson's Feed Cache package. It was pretty easy to install and use.

  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 15:02
    Rik Helsen
    0

    Thanks Frederik & Sebastian, I need to do some filtering too though, only displaying items from the rss feed that have one specific category...

    This is proving a challenge for my xslt knowledge (traversing through the xml structure in xslt), this is what i have not, but it's not managing to filter based on category (a rss item can have multiple ones)

    my current (not working) code:

    <?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:umbraco.library="urn:umbraco.library"
    xmlns:media="http://search.yahoo.com/mrss"
    exclude-result-prefixes="msxml umbraco.library media">
    <xsl:output method="xml" omit-xml-declaration="yes"/>

    <!-- parameters source of the RSS feed and Filtertag -->
    <xsl:variable name="url">http://www.orbitone.com/en/blog/rss.xml</xsl:variable>;
    <xsl:variable name="tag">Juniper</xsl:variable>


    <xsl:template match="/">
    <xsl:if test="$url != ''">
    <!-- fetch xmlsource from url -->
    <xsl:variable name="blogposts" select="umbraco.library:GetXmlDocumentByUrl($url)" />
    <ul id="rssblog">
    <xsl:apply-templates select="$blogposts//item" />
    </ul>
    </xsl:if>
    </xsl:template>


    <xsl:template match="item">
    <xsl:for-each select="./category">
    <xsl:if test="contains(., $tag)">
    <xsl:variable name="ShowCurrentItem" select="true"/>
    O
    </xsl:if>
    </xsl:for-each>

    <!-- <xsl:if test="contains(./category, $tag)">-->
    <xsl:if test="@ShowCurrentItem='true'">
    <li class="rssBlogPost">
    <a href="{link}" title="{title}" class="rssBlogLink">
    <xsl:value-of select="./title"/> ----- <xsl:value-of select="./category"/>
    </a>
    <span class="rssBlogDescription">
    <!-- <xsl:value-of select="./description" disable-output-escaping="yes" />-->
    </span>
    </li>



    </xsl:if>
    </xsl:template>

    </xsl:stylesheet>

    any help with getting this filtering on category is more tha appreciated, it's the fact that an item has multiple ones that i'm having an issue with...

  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 15:16
    Rik Helsen
    0

    Got it, here's my current solution:

    <?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:umbraco.library="urn:umbraco.library"
    xmlns:media="http://search.yahoo.com/mrss"
    exclude-result-prefixes="msxml umbraco.library media">
    <xsl:output method="xml" omit-xml-declaration="yes"/>


    <!-- parameters source of the RSS feed and Filtertag -->

    <xsl:variable name="url">http://www.orbitone.com/en/blog/rss.xml</xsl:variable>;
    <xsl:variable name="tag">.NET</xsl:variable>


    <xsl:template match="/">
    <xsl:if test="$url != ''">
    <!-- fetch xmlsource from url -->
    <xsl:variable name="blogposts" select="umbraco.library:GetXmlDocumentByUrl($url)" />
    <ul id="rssblog">
    <xsl:apply-templates select="$blogposts//item" />
    </ul>
    </xsl:if>
    </xsl:template>


    <xsl:template match="item">
    <xsl:for-each select="./category">
    <xsl:if test="contains(., $tag)">
    <li class="rssBlogPost">
    <a href="{../link}" title="{../title}" class="rssBlogLink">
    <xsl:value-of select="./../title"/>
    </a>
    <span class="rssBlogDescription">
    <xsl:value-of select="./../description" disable-output-escaping="yes" />
    </span>
    </li>
    </xsl:if>
    </xsl:for-each>


    </xsl:template>


    </xsl:stylesheet>
  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 28, 2010 @ 15:23
    Sebastiaan Janssen
    1

    Cool, be aware though, that this will cause a request to a remote server with each page load, which might be slow (or fail) and could cost a lot of bandwith on both ends.

    With Feed Cache, the content is being cached and only refreshed every so often. It still generate xml and can be filtered through XSLT. Something to consider..

  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 15:25
    Rik Helsen
    0

    Interesting, i'll give it a closer look!

    bandwidth isn't really an issue in our case, but page speed is.

  • Sebastiaan Janssen 5060 posts 15522 karma points MVP admin hq
    Jan 28, 2010 @ 15:29
    Sebastiaan Janssen
    0

    Come to think of it, I don't exactly remember what it does out of the box! But I did something funky so I could get access to the cached XML, putting it in an xsl:variable so I could loop over it. This is how:

    <xsl:variable name="rssFeed" select="document(concat(concat('../umbraco/plugins/FergusonMoriyama/feedcache/', $currentPage/@id), '.xml'))/rss" />

     

  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 16:35
    Rik Helsen
    0

    As soon as i have this set up i keep getting validation errors like these:

    Error occured
    System.Xml.XmlException: An error occurred while parsing EntityName. Line 4, position 106.

    at System.Xml.XmlTextReaderImpl.Throw(Exception e)
    at System.Xml.XmlTextReaderImpl.Throw(String res, String arg)
    at System.Xml.XmlTextReaderImpl.Throw(String res)
    at System.Xml.XmlTextReaderImpl.ParseEntityName()
    at System.Xml.XmlTextReaderImpl.ParseEntityReference()
    at System.Xml.XmlTextReaderImpl.Read()
    at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace)
    at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc)
    at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace)
    at System.Xml.XmlDocument.Load(XmlReader reader)
    at System.Xml.XmlDocument.LoadXml(String xml)
    at umbraco.library.GetXmlDocumentByUrl(String Url)

     

    And this error when i try to visualize the XSLT:


    Error parsing the XSLT:

    System.Xml.Xsl.XslTransformException: An error occurred during a call to extension function 'GetXmlDocumentByUrl'. See InnerException for a complete description of the error. ---> System.Xml.XmlException: An error occurred while parsing EntityName. Line 4, position 106. at System.Xml.XmlTextReaderImpl.Throw(Exception e) at System.Xml.XmlTextReaderImpl.Throw(String res, String arg) at System.Xml.XmlTextReaderImpl.Throw(String res) at System.Xml.XmlTextReaderImpl.ParseEntityName() at System.Xml.XmlTextReaderImpl.ParseEntityReference() at System.Xml.XmlTextReaderImpl.Read() at System.Xml.XmlLoader.LoadNode(Boolean skipOverWhitespace) at System.Xml.XmlLoader.LoadDocSequence(XmlDocument parentDoc) at System.Xml.XmlLoader.Load(XmlDocument doc, XmlReader reader, Boolean preserveWhitespace) at System.Xml.XmlDocument.Load(XmlReader reader) at System.Xml.XmlDocument.LoadXml(String xml) at umbraco.library.GetXmlDocumentByUrl(String Url) --- End of inner exception stack trace --- at System.Xml.Xsl.Runtime.XmlExtensionFunction.Invoke(Object extObj, Object[] args) at System.Xml.Xsl.Runtime.XmlQueryContext.InvokeXsltLateBoundFunction(String name, String namespaceUri, IList`1[] args) at (XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at Root(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at Execute(XmlQueryRuntime {urn:schemas-microsoft-com:xslt-debug}runtime) at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlSequenceWriter results) at System.Xml.Xsl.XmlILCommand.Execute(Object defaultDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter writer, Boolean closeWriter) at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, XmlWriter results) at System.Xml.Xsl.XmlILCommand.Execute(IXPathNavigable contextDocument, XmlResolver dataSources, XsltArgumentList argumentList, TextWriter results) at System.Xml.Xsl.XslCompiledTransform.Transform(IXPathNavigable input, XsltArgumentList arguments, TextWriter results) at umbraco.macro.GetXsltTransformResult(XmlDocument macroXML, XslCompiledTransform xslt, Dictionary`2 parameters) at umbraco.presentation.umbraco.developer.Xslt.xsltVisualize.visualizeDo_Click(Object sender, EventArgs e)

     

    The cached feed does render in a browser though, i've read here and there that this has to do with encoding of the xml content...

     

     

  • Rik Helsen 670 posts 873 karma points
    Jan 28, 2010 @ 16:37
  • Sander Houttekier 114 posts 163 karma points
    Feb 25, 2010 @ 22:45
    Sander Houttekier
    0

    any news on this as it seems a client of us came back with this error too

    it worked perfect begin february

    but seems like the xslt now returns the same error as rik helsen has.

     

    our feed is a feed from a flickr group

    feed seems ok, but i have no clue as to what changed (defenatly nothing in the xslt, it seems to be a change in the feed that triggers this, as we didn't do any updates of the umbraco either )

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 25, 2010 @ 23:54
    Lee Kelleher
    1

    Hi Rik,

    Taking your XSLT from above, here's my take on it:

    <?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:umbraco.library="urn:umbraco.library"
        xmlns:dc="http://purl.org/dc/elements/1.1/"
        exclude-result-prefixes="msxml umbraco.library dc">
        <xsl:output method="xml" omit-xml-declaration="yes"/>
    
        <!-- parameters source of the RSS feed and Filtertag -->
        <xsl:variable name="url">http://www.orbitone.com/en/blog/rss.xml</xsl:variable>;
        <xsl:variable name="tag">.NET</xsl:variable>
    
        <xsl:template match="/">
            <!-- check that the url isn't empty -->
            <xsl:if test="string-length($url) &gt; 0">
                <!-- get the xml from the url (with the cache, in seconds) -->
                <xsl:variable name="rss" select="umbraco.library:GetXmlDocumentByUrl($url, 3600)" />
                <!-- test if the RSS feed has any items -->
                <xsl:if test="count($rss/rss/channel/item[category = $tag]) &gt; 0">
                    <ul id="rssblog">
                        <!-- apply the templates for the 'item' element, (which have a 'category' element containing the value of $tag -->
                        <xsl:apply-templates select="$rss/rss/channel/item[category = $tag]" />
                    </ul>
                </xsl:if>
            </xsl:if>
        </xsl:template>
    
        <xsl:template match="item">
            <li class="rssBlogPost">
                <a href="{link}" title="{title}" class="rssBlogLink">
                    <xsl:value-of select="title"/>
                </a>
                <span class="rssBlogDescription">
                    <xsl:value-of select="description" disable-output-escaping="yes" />
                </span>
            </li>
        </xsl:template>
    
    </xsl:stylesheet>

    OK, here's what is different.

    • The "GetXmlDocumentByUrl" method has an overload to accept a cache expiry (in seconds), so that will give you some performance gains.
    • Once we get the RSS XML (in the $rss variable), we check if it has any <item> elements - coz if it doesn't then you'll get an empty <ul /> tag.
    • For the <xsl:apply-templates /> we add a conditional XPath to select only the <item> elements that have a <category> with the value of the $tag variable.
    • Then in the "match=item" template ... you can do whatever you need to in there!

    Obviously tweak it as you see fit.

    Good luck, Lee.

  • Lee Kelleher 4026 posts 15836 karma points MVP 13x admin c-trib
    Feb 25, 2010 @ 23:57
    Lee Kelleher
    0

    Hi Sander, if you can post your XSLT, we might be able to see what the problem is.

    My guess would be that an XML namespace is missing from your XSLT - but that's a guess.

    Thanks, Lee.

  • Peter Duncanson 430 posts 1360 karma points c-trib
    Nov 11, 2010 @ 20:15
    Peter Duncanson
    0

    I've been having this problem all damn day. Seems if you are using GetXmlDocumentByUrl in your templates and try to pass a variable through for the url it can choke. After 3 hours digging around I did a IIS restart out of desperation and its now cleared it. Something going on under the hood I'm just not sure what! So for anyone else who has it, try a IIS restart first to see if that clears it. More news as I have it.

    Cheers

    Pete

Please Sign in or register to post replies

Write your reply to:

Draft