Read and display another site's RSS feed on my site?
As the title says, is it possible to create an xslt macro that reads from a certain rss feed and display the content on my own page? I'd rather do it myself than use these free services.
Something like this should hopefully get you started:
<xsl:template match="/">
<xsl:variable name="feed" select="concat('http://www.rss-feed-url.com')"/>
<xsl:variable name="rssFeed">
<container>
<xsl:copy-of select="umbraco.library:GetXmlDocumentByUrl($feed)"/>
</container>
</xsl:variable>
<xsl:variable name="feedNodeSet" select="msxsl:node-set($rssFeed)/container/channel/item"/>
<xsl:for-each select="$feedNodeSet">
<!-- Do something here with each item in the feed -->
</xsl:for-each>
</xsl:template>
Ah, you can remove the concat function. I did this in a bit of a rush and it's pulled from some code I have which aggregates multiple RSS feeds. It can probably be simplified quite a lot to something like this (again, untested!)
<xsl:template match="/">
<xsl:variable name="rssFeed">
<xsl:copy-of select="umbraco.library:GetXmlDocumentByUrl('http://www.rss-feed-url.com')"/>
</xsl:variable>
<xsl:variable name="feedNodeSet" select="msxsl:node-set($rssFeed)/channel/item"/>
<xsl:for-each select="$feedNodeSet">
<!-- Do something here with each item in the feed -->
</xsl:for-each>
</xsl:template>
Here it is, It seems that I can read the feed. and display it entirely using what you just said (btw there seems to be a problem with posting code snippets). It won't loop through each element though.
I've tweaked and tested and this should work. Just need the 'RSS' element to be considered in the loop:
<xsl:template match="/">
<!-- Set feed URL -->
<xsl:variable name="feedURL" select="'http://hsfo.dk/section/artikler/?template=RSS&mime=xml&profile=1196'"/>
<!-- Check feed is not empty (obviously not required if you're hard-coding the feed as above -->
<xsl:if test="normalize-space($feedURL)">
<!-- Assign feed to a variable -->
<xsl:variable name="rssFeed">
<xsl:copy-of select="umbraco.library:GetXmlDocumentByUrl($feedURL)"/>
</xsl:variable>
<!-- Convert feed variable to a node-set so we can work with it -->
<xsl:variable name="feedNodeSet" select="msxsl:node-set($rssFeed)"/>
<!-- Loop through each item in the node-set -->
<xsl:for-each select="$feedNodeSet/rss/channel/item">
<!-- Output the data, for example the title: -->
<p><xsl:value-of select="title" /></p>
</xsl:for-each>
</xsl:if>
</xsl:template>
There may be a better way to do this, but I think that by using 'local-name' you can bypass the namespace part of the element, so something like this should work:
<!-- Loop through each item in the feed -->
<xsl:for-each select="$feedNodeSet/rss/channel/item">
<!-- Output the data, for example the title: -->
<p><xsl:value-of select="title" /></p>
<xsl:for-each select="./*[local-name()='thumbnail']">
<img>
<xsl:attribute name="src">
<xsl:value-of select="@url" />
</xsl:attribute>
</img>
</xsl:for-each>
</xsl:for-each>
This should also cater for multiple thumbnail images, which I seem to remember is allowed within the spec.
Read and display another site's RSS feed on my site?
As the title says, is it possible to create an xslt macro that reads from a certain rss feed and display the content on my own page? I'd rather do it myself than use these free services.
Hi Alexandru,
Something like this should hopefully get you started:
I will see if I can put something together today. I will keep the post updated. Thanks!
What would the second argument be?
This says there's only one argument as well as you do.
Ah, you can remove the concat function. I did this in a bit of a rush and it's pulled from some code I have which aggregates multiple RSS feeds. It can probably be simplified quite a lot to something like this (again, untested!)
It won't read the feed apparently.
I have just added the link to the feed and some text to print for each item and nothing happens.
What do you think?
I think this is how I am supposed to loop through all items
However, it still shows nothing... it seems to avoid the for-each loop
Can you post the URL for the RSS feed? It may be using a different structure.
You could also see the structure of the feed that you're bringing in by outputting its content to a textarea, by changing this:
to this:
Here it is, It seems that I can read the feed. and display it entirely using what you just said (btw there seems to be a problem with posting code snippets). It won't loop through each element though.
I've tweaked and tested and this should work. Just need the 'RSS' element to be considered in the loop:
Aye. I have just realized that too just before you posted hehe. Thanks. Ill mark your solution ;)
One last thing.
all items contain this:
<media:thumbnail url="http://JMimg.no.publicus.com/storyimage/JM/20131008/ARTIKLER/131009553/AR/0/AR-131009553.jpg" height="2848" width="4288" />
Any idea how to render this on my page?
There may be a better way to do this, but I think that by using 'local-name' you can bypass the namespace part of the element, so something like this should work:
This should also cater for multiple thumbnail images, which I seem to remember is allowed within the spec.
You rock 2x! ;)
is working on a reply...