Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

skip an item in an rss feed

Status
Not open for further replies.

zerkat

Programmer
Jul 12, 2007
103
US
Hi - I have an rss feed for local weather on a site. Everything works well but within the xml for the feed there is an advertisement which my company does not want displayed. How can I tell it not to display that particular item from the rss feed?
 
what ive done with a news rss before was i found there was a div ID that contained "advertisements", and by viewing the source, i was able to isolate that div and then used a simple #divIdName { display: none; } in my stylesheet.

do you have the ability to modify the xslt?
if so, then we might be able to do some filtering in there as opposed to using css. It depends on how they organized the xml. can you post a sample xml output?
 
Thanks Adamroof. You sound like you know more about XML than I do - this what I have right now.

I am using XPath to display the rss feed:

<asp: XMLDataSource ID = "LocalWeatherXMLDataSource" runat="server" Datafile="~/rss/weather.xml" XPath="rss/channel/item">
</asp:XmlDataSource>

<div class="panels">
<asp:panel ID="rssPanel" runat="server" Height="200px" Width="200px" BackColor="WhiteSmoke" ScrollBars="Auto" CssClass="panels">

<asp:DataList ID="DataList1" runat="server" DataSourceID="LocalWeatherXMLDataSource">

<ItemTemplate>
<a href="<%#XPath("link") %>"> <%#XPath("title") %> </a> <br />
<%#XPath("description") %>
<hr />
</ItemTemplate>
</asp:DataList>
</asp:panel>

And the actual rss feed is coming from the weather channel's website - they wanted local weather feed. The URL to get to the feed is The link they don't want displayed is the ADV link. Right now I have it going to a file because this site is in development right now with no internet access.
 
sorry for the late reply, if you are still looking for a solution, you can try this. I wasnt able to recreate your sample using the DataList, but try this out and see if you like it.

HTML
-----
add <asp:Literal ID="rssWeather" runat="server" /> where you want it to show

XSLT
-----
I put this in an xml subfolder, but can go in the root if you want. This will strip your adverstisements based off of keywords you can change.
Save as xml/weather.xslt
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
    <xsl:output method="html" />

    <xsl:template match="rss/channel">
        <!--Gets the top 5 records-->
        <xsl:for-each select="item[position()&lt;=5]">
            <xsl:choose>
                <!--Check if the title has the keyword ADV or Video in it-->
                <xsl:when test="contains(title,'ADV') or contains(title,'Video')">
                    <div>I Removed This Item!</div>
                </xsl:when>
                <!--If not, show a formatted item-->
                <xsl:otherwise>
                    <div style="border-bottom: dotted 1px Gray;padding-top: 5px; padding-bottom: 3px;">
                        <div>
                            <a href="{link}" target="_blank">
                                <xsl:value-of select="title" />
                            </a>
                        </div>
                        <div style="margin-left: 4px;" class="rssDescription">
                            <xsl:value-of disable-output-escaping="yes" select="description" />
                        </div>
                    </div>
                </xsl:otherwise>
            </xsl:choose>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>


C# Codebehind
-----
add this routine
Code:
private void GetFeed(string strURL, string xsltFilePath, Literal outputLocation)
{
    try
    {
        string xsltFile = Server.MapPath(xsltFilePath);
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(strURL);
        XslTransform xslDoc = new XslTransform();
        xslDoc.Load(xsltFile);

        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);

        xslDoc.Transform(xmlDoc, null, sw);
        outputLocation.Text = sb.ToString();
    }
    catch (Exception ex)
    {
        throw new Exception(ex.Message);
    }
}

Then call the feed from the page load event

Code:
    protected void Page_Load()
    {
        if (!IsPostBack)
        {
            GetFeed("[URL unfurl="true"]http://rss.weather.com/weather/rss/local/21727?cm_ven=LWO&cm_cat=rss&par=LWO_rss",[/URL]
                "xml/weather.xslt", rssWeather);
        }
    }

If you have the xml file downloaded for offline, replace the call to
GetFeed("~/rss/weather.xml","xml/weather.xslt", rssWeather);
i couldnt test that, but hopefully it works out for you.


To create another feed, just add another <asp:Literal and pass in another URL and either the same xslt or create another one.

GetFeed(" "xml/rss.xslt", literalID);
 
Hi Adamroof,

Thanks for getting back to me. Sorry it took me so long to get back to you. These holidays are a real killer. I will keep this code for the future. Management decided to remove the rss feed completely.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top