Copied to clipboard

Flag this post as spam?

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


  • organic 108 posts 157 karma points
    Oct 21, 2010 @ 22:05
    organic
    0

    RSS feed shows as one big string

    I am trying to display the first few 'titles' from 'items' in this RSS feed:  http://www.nll.com/feed.php

    This displays correctly on an old website using this XSLT with ASP.NET xml control(<asp:Xml>):

    <?xml version="1.0" encoding="iso-8859-1" ?>
    <xsl:stylesheet
        version="1.0"
        xmlns:rss="http://purl.org/rss/1.0/"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
      <xsl:output omit-xml-declaration="yes" />
      <xsl:template match="rss|/rdf:RDF">
        <feed>
          <xsl:apply-templates select="channel/item|/rdf:RDF/rss:item" />
        </feed>
      </xsl:template>
      <xsl:template match="channel/item|/rdf:RDF/rss:item">
        <xsl:if test="position()&lt; 5">
          <tr>
            <td style="vertical-align: top">
              <a target="_blank" href="{link|rss:link}" class="smallred">
                <xsl:value-of select="title|rss:title" disable-output-escaping="yes" />
              </a></td>
          </tr>
        </xsl:if>
      </xsl:template>
    </xsl:stylesheet>

    But I can't get this working in Umbraco, the best I can come to getting this working is just displaying the whole feed as one big string of text without the XML tags, not sure why that happens..  I've tried starting with samples(RunwayFeedViewer, LastFM,...). I think what I am having a hard time doing is correctly passing the feed to the template which people seem to be done with umbraco.library:GetXmlDocumentByUrl().

    Can anyone help?

    Thanks

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 21, 2010 @ 22:19
    Jan Skovgaard
    0

    What is the source of your RSS feed? you should pass it to the umbraco.library:GetXmlDocumentByUrl() as a string like this: umbraco.library:GetXmlDocumentByUrl('theurlofyourfeed') - this should be done if your feed comes from an external source.

    Hope this helps

    /Jan

  • organic 108 posts 157 karma points
    Oct 21, 2010 @ 22:36
    organic
    0

    The feed is stated above: http://www.nll.com/feed.php

    I have tried using umbraco.library:GetXmlDocumentByUrl('http://www.nll.com/feed.php') and, like I said, the only content I can render is the entire feed as one big string with no XML tags.  It seems that it is not parsing.

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Oct 21, 2010 @ 22:51
    Jan Skovgaard
    1

     

    Ok, how do you determine that it's returning a string?

    if you are using <xsl:value-of select="umbraco.library:GetXmlDocumentByUrl('http://www.nll.com/feed.php')" /> then it returns a string and then it can be a bit tough to figure out, which elements and attributes to match.

    I will recommend that you write the following in your XSLT.

    <textarea>

    <xsl:copy-of select="umbraco.library:GetXmlDocumentByUrl('http://www.nll.com/feed.php')" />

    </textarea>

    By using copy-of, you get the returned XML.

    Copy the entire XML output to an xml document, so you can see the whole structure. Now you should be able to figure out how to parse the XML.

    /Jan

  • organic 108 posts 157 karma points
    Oct 21, 2010 @ 23:11
    organic
    0

    Thanks Jan,

    I think I was testing the results of the GetXmlDocumentByUrl like you said, but otherwise I'm getting no results...that's my problem ;-)  I cannot seem to 'connect' the $feed variable to the XSLT that worked on the old site, which seems like the right goal.(I will be gone for the weekend in 1 hour, thanks for your help)

    Here is the whole template:

     <xsl:template match="/">

        <xsl:variable name="Feed" select="umbraco.library:GetXmlDocumentByUrl('http://www.nll.com/feed.php')" />
        <ul>
        <xsl:value-of select="$Feed" />

         <xsl:for-each select="$Feed//item">
          <xsl:if test="position() &lt;= $NumberOfItems">
           <xsl:apply-templates select="." />

          </xsl:if>
         </xsl:for-each>
        </ul>
     </xsl:template>
     
     <!-- Template: item -->

     <xsl:template match="item">
      <li>
       <a href="{link}"><xsl:value-of select="title"/></a>
      </li>

     </xsl:template>

  • Kim Andersen 1447 posts 2196 karma points MVP
    Oct 23, 2010 @ 00:16
    Kim Andersen
    0

    Hi organic

    I took a look at this in one of my v4.5.2 installations, and I did find a solution even though I think it's a bit weird. Take a look at the following code:

    <xsl:template match="/">
        <xsl:variable name="Feed" select="umbraco.library:GetXmlDocumentByUrl('http://www.nll.com/feed.php')" />
        <ul>
          <xsl:for-each select="$Feed//*[name()='item']">
             <xsl:if test="position() &lt;= 5">
               <li><a href="{./*[name()='link']}"><xsl:value-of select="./*[name()='title']" /></a></li>
             </xsl:if>
            </xsl:for-each>
        </ul>
     </xsl:template>

    This actually outputs five list elements on my page with the right title and link so that's nice and solves your problem.

    But what I don't understand is why I had to use the wildcard selector (*) and then narrow it down using the name(). I my mind the following code should output the same:

    <xsl:for-each select="$Feed//item">
         <xsl:if test="position() &lt;= 5">
             <li><a href="{./link}"><xsl:value-of select="./title" /></a></li>
         </xsl:if>
    </xsl:for-each>

    But the above code doesn't output anything. I tried making some small changes in the code as well, but still with no luck. So if any of you XSLT gurus out there have an explanation I'd like to hear it :)

    Anyways, the first snippet I wrote in the top should output items from the RSS-feed that you provided if you are using the new XML schema. Give it a shoot when you're back from the weekend and let us know if it works :)

    /Kim A

  • organic 108 posts 157 karma points
    Oct 25, 2010 @ 23:02
    organic
    0

    Tired from my 6:15am (GMT-8?) flight, but very happy to see that Kim has helped me out, thank you!  As you'd expect, since your XSLT works for you, it is also working for me in my 4.0.4.1 installation. I need to get better at XPath.  Still, one weird thing is the decoding of the "&amp;amp;" in the first few feed items.  I don't know why the feed is using those characters for "&" instead of just one "&amp;"  I thought Umbraco.Library.HTMLDecode would help but there is only an HTMLEncode method.  That seems strange.


    Anyway, thanks for the help.

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 26, 2010 @ 01:04
    Chriztian Steinmeier
    1

    @Kim: 

    The reason the two versions don't do the same has to do with namespaces - specifically default namespaces, and it's a little tricky to explain, but for now here's a quick try:

    <root xmlns:demo="http://xmlns.greystate.dk/2010/demo-namespace">
    
        <item xmlns="http://xmlns.greystate.dk/2010/demo-namespace">
            I will NOT match //item
            I WILL match //*[name() = 'item']
            I WILL match //*[local-name() = 'item']
            I WILL match //demo:item
        </item>
    
        <demo:item>
            I will NOT match //item
            I will NOT match //*[name() = 'item']
            I WILL match //*[local-name() = 'item']
            I WILL match //demo:item
        </demo:item>
    
    </root>
    
    See - the namespace (the URL doesn't matter, only that the two are exactly the same) is assigned the prefix "demo" on the root element, but on the first <item> element, the same 'namespace-uri' is used as the default namespace (without a prefix) so the item element belongs to that namespace.
    On the second <item> the prefix is used so that also belongs to that same namespace.
    Sometimes it's visualized by saying that the XML parser passes both of the item elements on like this:
    <{http://xmlns.greystate.dk/2010/demo-namespace}item>
    Namespaces can be hard to "grok" at first, but they are actually a very clever thing (at least I think so :-) to get to grips with. 
    The fact that the first item will actually match the "demo:item" expression, trips a lot of people up...

    OK, I'll shut up now... :-)

    /Chriztian 

  • Kim Andersen 1447 posts 2196 karma points MVP
    Oct 26, 2010 @ 08:46
    Kim Andersen
    0

    That's a nice explanation Chriztian. And it actually makes sense. Never thought of this, but after your post I understand why - well, at least I think I understand why :D

    If the time allows it, this could maybe be a small topic at the XSLT Ping Pont Fest? ;)

    Anyways, thanks for clearing things up!

    /Kim A

  • Chriztian Steinmeier 2800 posts 8790 karma points MVP 8x admin c-trib
    Oct 26, 2010 @ 09:13
    Chriztian Steinmeier
    0

    -- To me, that's exactly what the "XSLT Ping Pong Fest" is all about :-)

    /Chriztian

Please Sign in or register to post replies

Write your reply to:

Draft