[英]Namespace prefix and cdata tags in xml generation for rss feed in Ruby on Rails
我需要為一個看起來大致如下的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") }
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.