Ruby XML Builder, how to create this namespace? - ruby-on-rails

I'm using Ruby's XML Builder and trying to find the proper syntax to recreate the following RSS 2.0 declaration:
<rss version="2.0"
xmlns:g="http://base.google.com/ns/1.0">
What's the appropriate way of going about this with XML Builder to put together the above block?

Try this builder script:
xml.rss :version => "2.0", "xmlns:g" => "http://base.google.com/ns/1.0" do
end
This will yield:
<rss xmlns:g="http://base.google.com/ns/1.0" version="2.0">
</rss>

Related

Builder GEM XML formatting

I'm using the Ruby Gem Builder, and I need this output..
<?xml version="1.0" encoding="utf-8"?>
<fileAttachment> "name of file here.xls"
<Data>zip</Data>
<Size>7434</Size>
</fileAttachment>
My code is below, but the file name next to "fileAttachment" just isn't working.. This is something simple I'm just not seeing it?? The error says I can't mix text with a block.. makes sense I just don't know the correct syntax.
xml = Builder::XmlMarkup.new(:indent => 2 )
xml.instruct! :xml,:version=>"1.0", :encoding => "utf-8"
xml.fileAttachment("name of file here.xls") do
xml.Data "zip"
xml.Size "7434"
end
I think you want to use the text! method:
xml.fileAttachment do
xml.text! "name of file here.xls"
xml.Data "zip"
xml.Size "7434"
end

Generating iTunes XML nodes in RSS feed using Ruby/Rails

Q: How do I generate the XML nodes specific to iTunes using Ruby/Rails?
Trying to generate iTunes XML feed, e.g. (based off example):
xml.instruct! :xml, :version => "1.0"
xml.rss(:version => "2.0") do
xml.channel do
xml.title "Your Blog Title"
xml.description "A blog about software and chocolate"
xml.link posts_url
#posts.each do |post|
xml.item do
xml.title post.title
xml.description "Temporary post description"
xml.pubDate post.created_at.to_s(:rfc822)
xml.link post_url(post)
xml.guid post_url(post)
end
end
end
end
Which happily generates something like:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Your Blog Title</title>
<description>A blog about software and chocolate</description>
<link>https://pubweb-thedanielmay.c9.io/sermons</link>
<item>
... omitted ...
</item>
</channel>
</rss>
But looks like I need to generate iTunes-specific XML nodes (as per Working With iTunes, e.g.)
<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> <-- *
<channel>
<title>Your Blog Title </title>
... omitted ...
<itunes:subtitle>A program about everything</itunes:subtitle> <-- *
... etc ...
Not sure how I generate the iTunes-specific nodes as they have colons in them.
Standard RSS nodes are like:
xml.item --> <item>
How do I get to generating nodes like:
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0">
<itunes:author>
Ah, answers as per code via Ryan Bates' awesome Railscasts RSS sample
xml.rss "xmlns:itunes" => "http://www.itunes.com/dtds/podcast-1.0.dtd", :version => "2.0"
and
xml.itunes :author, author

Insert Current Date/Time into XML through Ruby Builder

I want to insert the current date/time into my xml feed using Rails Builder.
Here is what I am trying to get:
<?xml version="1.0" encoding="utf-8" ?>
<FirstObject timestamp="2002-06-01T07:00:00">
....
I have:
xml.instruct!
xml.FirstObject
....
What is the command to put in the current date? Thanks!
xml.FirstObject :timestamp => Time.now

Rendering XML with Stylesheet in Rails

I can't figure out how to get a rendered collection (as XML) to include a style sheet line such as:
<?xml-stylesheet type="text/xsl" href="example.xsl" ?>
This guy says to add a proc as such:
proc = Proc.new { |options| options[:builder].instruct!(:xml-stylesheet, type=>'text/xsl', :href=>'something.xsl') }
#foo.to_xml :procs => [proc]
But I can't get that to work. Any suggestions?
Take a look at Nokogiri gem: http://nokogiri.org/Nokogiri/XSLT/Stylesheet.html
doc = Nokogiri::XML(File.read('some_file.xml'))
xslt = Nokogiri::XSLT(File.read('some_transformer.xslt'))
puts xslt.transform(doc)
You can use the atom_feed helper which is included with Rails:
atom_feed(instruct: {
'xml-stylesheet' => {type: 'text/xsl', href: 'styles.xml'}
}) do |feed|
feed.title "My Atom Feed"
# entries...
end
Which results in (showing only first 3 lines):
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="styles.xml"?>
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom">
Or, if not rendering an atom_feed, you can simply use instruct inside your builder:
xml.instruct! 'xml-stylesheet', href: 'style.xml', type: 'text/xsl'

rails rss namespace tag generation

<title>Webisode 200 Playlist</title>
<item>
<title>Testing 201</title>
<media:credit role="author">Adam Houston</media:credit>
<media:content url="http://www.youtube.com/watch?v=gb0bGZ06vXn" type="video/x-flv" />
<jwplayer:duration>200</jwplayer:duration>
</item>
i'm using rails xml to generate the corresponding xml but my question is how do i generate tags with namespace like or also how do i tell xml about the attirbutes like role="author".
Regards,
use the tag! method if you want to create such tags:
xml.tag!("SOAP:Envelope") # => <SOAP:Envelope/>
attributes are passed as hash options:
xml.target("name"=>"compile") # => <target name="compile"\>
see the documentation for more infos: http://ap.rubyonrails.org/classes/Builder/XmlMarkup.html#M000157

Resources