Transformation for reading RSS

Robyn Delk asked on January 28, 2016 00:19

I'm trying to read an RSS feed with an RSS Data Source. Here is a sample of the data from the feed:

<item>
  <title><![CDATA[ Charitable Giving and Children: Inspiring Kids to Give Back ]]></title>
  <image>
    <url>/getattachment/fb667719-f176-4137-9293-eb20f29f829e/Charitable-Giving-and-Children-Inspiring-Kids-to-G</url>
  </image>
  <author>Kara L. Rogers</author>
</item>

Most fields in my Repeater's Transformation are working fine. For example, I can read the title and author items in a transformation like this:

<%# Eval("Title") %>

How can I grab the url node inside the image node? I have tried:

<%# Eval("Image") %>
<%# Eval("Url") %>
<%# Eval("Image[Url]") %>

And probably a few others, including some XPath notation. Is there a way to grab that path from that XML in this transformation?

Thanks!

Recent Answers


Brenden Kehren answered on January 28, 2016 01:48

Use a placeholder to show/hide the image node. In this instance, if there is no URL don't show the image node.

<asp:PlaceHolder ID="phUrl" runat="server" Visible='<%# If(Eval<string>("Url") == "", false, true) %>'>
  <image>
    <url><%# Eval("Url") %></url>
  </image>
</asp:PlaceHolder>
0 votesVote for this answer Mark as a Correct answer

Robyn Delk answered on January 28, 2016 19:11

Thank you for responding, Brenden. The issue isn't that we are having a problem hiding/showing. The issue is that we can't get the transformation to read the data inside the sub-element of URL in an Image tag from this RSS feed. The example code....

<item>
  <title><![CDATA[ Charitable Giving and Children: Inspiring Kids to Give Back ]]></title>
  <image>
        <url>/getattachment/fb667719-f176-4137-9293-eb20f29f829e/Charitable-Giving-and-Children-Inspiring-Kids-to-G
        </url>
  </image>
  <author>Kara L. Rogers</author>
</item>

...shows that there is some tag nesting happening(https://validator.w3.org/feed/docs/rss2.html). How do we get the transformation to see through the nested tags?

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on January 28, 2016 20:27

I apoligize, I misunderstood your post then. The short answer is to use a nested repeater. I was not able to get a Kentico Basic Repeater to work in my case so I used a standard <asp:Repeater> control.

Here's the long answer with some code samples.

Here's my XML:

<featured_rates>
    <GROUPS>
        <GROUP>
            <NAME>FIXED RATE PRODUCTS</NAME>
            <PRODUCT>
                <DESCR>30 Year Fixed Rate</DESCR>
                <RATE>3.875</RATE>
                <APR>3.919</APR>
                <POINTS>0</POINTS>
                <PAYMENT_STREAM_URL>
                    https://URL.com/PaymentStream.aspx?CobranderId=1002&CriteriaId=119814037&ResultId=19
                </PAYMENT_STREAM_URL>
            </PRODUCT>
        </GROUP>
    </GROUPS>
</featured_rates>

Here is what I did with a plain <asp:Repeater> and an <asp:XMLDataSource>:

    <asp:XmlDataSource ID="dsXml" runat="server" />
    <asp:Repeater ID="rptItems" runat="server" DataSourceID="dsXml">
    <HeaderTemplate>
            <table class="graytable">
        <tbody>
    </HeaderTemplate>
    <ItemTemplate>
            <tr>
            <td colspan="3"><h3><%# XPath("NAME")%></h3></td>
            </tr>
            <asp:Repeater ID="rptReplacementFunds" runat="server" DataSource='<%# XPathSelect("PRODUCT") %>'>
                <ItemTemplate>
                    <tr>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>colspan="3"><h4><%# XPath("DESCR") %></h4></td>
                    </tr>
                    <tr>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>>Rate</td>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>>APR</td>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>>Points</td>
                    </tr>
                    <tr>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>><%# XPath("RATE") %>%</td>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>><%# XPath("APR") %>%</td>
                    <td <%# (Container.ItemIndex % 2 == 0 ? " class='even' " : "") %>><%# XPath("POINTS") %>%</td>
                    </tr>
                </ItemTemplate>
            </asp:Repeater>
    </ItemTemplate>
    <FooterTemplate>
            <tbody>
    </table>
    </FooterTemplate>
    </asp:Repeater>

Code behind:

/// <summary>
/// On content loaded override.
/// </summary>
public override void OnContentLoaded()
{
    base.OnContentLoaded();
    SetupControl();
}

/// <summary>
/// Initializes the control properties.
/// </summary>
protected void SetupControl()
{
    if (StopProcessing)
    {
        // Do nothing
    }
    else
    {
        dsXml.DataFile = DataFile; // this is the URL of the XML feed
        dsXml.XPath = DataXPath; // this is the level in the XML I want to start selection from.  In my case //FEATURED_RATES/GROUPS/GROUP
    }
}
0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.