加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – Ruby on Rails中rss feed的xml生成中的命名空

发布时间:2020-12-16 22:59:41 所属栏目:百科 来源:网络整理
导读:我需要为一个看起来大致如下的feed生成一个xml: – ?xml version="1.0" encoding="iso-8859-1" ?rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:atom="http://www.w3.org/2005/Atom"channel item g:id![CDATA[id]]/g:id title![CDATA[Pr
我需要为一个看起来大致如下的feed生成一个xml: –
<?xml version="1.0" encoding="iso-8859-1" ?>
<rss version="2.0" xmlns:g="http://base.google.com/ns/1.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
    <item>
        <g:id><![CDATA[id]]></g:id>
        <title><![CDATA[Product Name]]></title>
        <description><![CDATA[This should be a relatively detailed description with as little formatting as possible.]]></description>
        <g:brand>Brand X</g:brand>
        <g:sale_id>new</g:sale_id>
    </item>
    <item>
        Next product...
    </item>
</channel>
</rss>

我的代码目前看起来像这样: –

xml=Builder::XmlMarkup.new(:indent => 3)
xml.instruct!
xml.rss("version" => "2.0","xmlns:g" => "http://base.google.com/ns/1.0","xmlns:atom" => "http://www.w3.org/2005/Atom"){
xml.channel{
# remove xml.namespace = xml.namespace_definitions.find{|ns|ns.prefix=="atom"}
sale_products.each do |sp|
  sid = (products_info[sp.product_id]["sale_id"]).to_s()
  xml.item {
    #xml.id{ |xml| xml.cdata!(products_info[sp.product_id].own_id) }
    #xml.g :id,{ xml.cdata!("sdaf") }
    xml.product_title{ |xml| xml.cdata!(products_info[sp.product_id].name) }
    xml.description{ |xml| xml.cdata!(ActionController::Base.helpers.strip_tags(products_info[sp.product_id].description)) }
    xml.item {
      xml.brand { |xml| xml.cdata!(products_info[sp.product_id].designer_1) }
      xml.sale_id{ |xml| xml.cdata!(sid) }
    }
  }

end
}
}

我的问题是让名称空间前缀和cdata标签同时工作.

xml.g :id,"fdsafsad"

这将获取namesapce前缀.

xml.product_title{ |xml| xml.cdata!(products_info[sp.product_id].name) }

这会在值周围获得cdata标记.

xml.g :id,{ xml.cdata!("sdaf") }

这无法做到这一点.

如何同时为同一标记获取名称空间前缀和cdata标记.我究竟做错了什么?

编辑: – 我目前得到的输出如下: –

<g:id>
   <![CDATA[10005-0003]]>
</g:id>

我想要的输出应该只有cdata标签内的值(没有换行符等): –

<g:id><![CDATA[10005-0003]]></g:id>

请注意,我不想删除:indent => 3创建标记时,以便根据需要格式化其他标记.

解决方法

xml.tag!("g:id") { xml.cdata!("sdaf") }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读