Copied to clipboard

Flag this post as spam?

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


  • Alexandru 112 posts 272 karma points
    Oct 07, 2013 @ 09:29
    Alexandru
    0

    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.

  • Dan 1288 posts 3921 karma points c-trib
    Oct 07, 2013 @ 20:49
    Dan
    1

    Hi Alexandru,

    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>
    
  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 08:29
    Alexandru
    0

    I will see if I can put something together today. I will keep the post updated. Thanks!

  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 09:27
    Alexandru
    0

    Error occured System.Xml.Xsl.XslLoadException: Function 'concat()' must have at least 2 argument(s).

    What would the second argument be?

    This says there's only one argument as well as you do.

  • Dan 1288 posts 3921 karma points c-trib
    Oct 08, 2013 @ 09:37
    Dan
    0

    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>
    
  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 09:45
    Alexandru
    0

    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?

  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 09:50
    Alexandru
    0

    I think this is how I am supposed to loop through all items

    <xsl:for-each select="msxsl:node-set($rssFeed)/channel/item">
    Test Print </xsl:for-each>
    

    However, it still shows nothing... it seems to avoid the for-each loop

  • Dan 1288 posts 3921 karma points c-trib
    Oct 08, 2013 @ 09:59
    Dan
    0

    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:

    <xsl:variable name="feedNodeSet" select="msxsl:node-set($rssFeed)/channel/item"/>
    

    to this:

    <xsl:variable name="feedNodeSet" select="msxsl:node-set($rssFeed)"/>
    <textarea><xsl:copy-of select="$feedNodeSet" /></textarea>
    
  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 10:05
    Alexandru
    0
      <xsl:copy-of select="umbraco.library:GetXmlDocumentByUrl('http://hsfo.dk/section/artikler/?template=RSS&amp;mime=xml&amp;profile=1196')"/>
    

    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.

  • Dan 1288 posts 3921 karma points c-trib
    Oct 08, 2013 @ 10:47
    Dan
    100

    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&amp;mime=xml&amp;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>
    
  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 10:50
    Alexandru
    0

    Aye. I have just realized that too just before you posted hehe. Thanks. Ill mark your solution ;)

  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 10:56
    Alexandru
    0

    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?

  • Dan 1288 posts 3921 karma points c-trib
    Oct 08, 2013 @ 11:31
    Dan
    1

    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.

  • Alexandru 112 posts 272 karma points
    Oct 08, 2013 @ 11:35
    Alexandru
    0

    You rock 2x! ;)

Please Sign in or register to post replies

Write your reply to:

Draft