简体   繁体   English

解析包含多个的atom / rss feed <link> Haml在RoR上添加标签

[英]Parsing atom/rss feed containing multiple <link> tags with Haml on RoR

So, firstly, here's an Atom feed snippet which I am trying to parse: 因此,首先,这是我要解析的Atom提要片段:

// http://somelink.com/atom   
<feed xmlns="http://www.w3.org/2005/Atom">
    <entry>
        <title>Title Here</title>
        <link href="http://somelink.com/link1&amp;amp;ref=rss" rel="alternate" />
        <link href="http://somelink.com/link2&amp;amp;ref=rss" rel="tag:somelink.com/apply_url"/>
    ...
    </entry>

I pull the Atom feed like so, 我像这样拉动Atom提要,

// In controller index method
@rss = SimpleRSS.parse open('http://somelink.com/atom')

Then I output the response in the view, which I am writing using Haml, as follows: 然后,我在使用Haml编写的视图中输出响应,如下所示:

- @rss.entries.each do |item|
  .title-div
    = item.title
  .title-link
    = item.link //outputs the first link

I could run a second loop for the links but is there a way to get the second link without it? 我可以为链接运行第二个循环,但是有没有办法获得第二个链接呢? Like reading the "rel" attribute and outputting the correct link? 就像读取“ rel”属性并输出正确的链接一样? How do I do this in Haml/Rails? 如何在Haml / Rails中做到这一点?

EDIT: The gem i am using: http://simple-rss.rubyforge.org/ 编辑:我正在使用的宝石: http : //simple-rss.rubyforge.org/

我不熟悉该gem,但是您是否尝试过item.links来查看每个项目是否提供了链接的集合?

I have never used SimpleRSS but maybe you could give Nokogiri or Hpricot a try? 我从未使用过SimpleRSS,但也许您可以尝试一下NokogiriHpricot You can than run an XPath query to only select the link with the right attribute. 然后,您可以运行XPath查询以仅选择具有正确属性的链接。 An example with Nokogiri: Nokogiri的示例:

atom_doc = Nokogiri::XML(open("http://www.example.com/atom.xml"))
atom_doc.xpath("/xmlns:feed/xmlns:entry/xmlns:link[@rel='tag:somelink.com/apply_url']")

Don't forget the namespaces if you are parsing an Atom feed. 如果您正在解析Atom提要,请不要忘记名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM