Namespace prefix and cdata tags in xml generation for RSS feed in Ruby on Rails

I need to create xml for a feed that looks something like this: -

<?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> 

Currently my code looks something like this: -

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

My problem is to use both namespace prefixes and cdata tags at the same time.

 xml.g :id, "fdsafsad" 

This gets the namesapce prefix.

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

This gets cdata tags around the values.

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

This will not help.

How to get both a namespace prefix and cdata tags that work simultaneously for the same tag. What am I doing wrong?

Edit : - The result that I am getting now is similar: -

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

The result I want should have a value inside the cdata tags (no newline, etc.): -

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

Note that I do not want to remove: indent => 3 when creating markup, so that other tags are formatted as needed.

+6
source share
1 answer
 xml.tag!("g:id") { xml.cdata!("sdaf") } 
+8
source

Source: https://habr.com/ru/post/925896/


All Articles