ruby-on-rails - Ruby on Rails 中 rss 提要的 xml 生成中的命名空间前缀和 cdata 标记

标签 ruby-on-rails ruby xml rss nokogiri

我需要为提要生成一个 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"

这会获取命名空间前缀。

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") }

关于ruby-on-rails - Ruby on Rails 中 rss 提要的 xml 生成中的命名空间前缀和 cdata 标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12525220/

相关文章:

ruby-on-rails - RSPEC 让替代范围界定

ruby-on-rails - benchmark.rb 中的实时时间可以小于其他时间吗?

ruby-on-rails - 向 content_tag 添加空白属性

ruby-on-rails - 无法复制 NilClass - 错误

ruby-on-rails - to_formatted_s(:long_ordinal) returning 00:00 at the end

xml - 错误 : No persistence units parsed from {classpath*:META-INF/persistence. xml}

php - SimpleXML 和 PHP : Extract part of XML document & convert as Array

java - 与 XML 配置相比,为什么 Java Config 在 spring boot 中更受欢迎?

ruby-on-rails - 如何在Rails View 中注释代码?

javascript - Epic 编辑器无法访问我的文本区域 rails