Insert Current Date/Time into XML through Ruby Builder - ruby-on-rails

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

Related

Declaring XML Tags in Ruby

I am using Ruby to pull information from an excel sheet and with this information produce an xml file. I need to produce this in Ruby:
What I want:
<Betrag waehrung="EUR">150000</Betrag>
What I have:
<Betrag waehrung ="EUR"/>
I am currently trying xml.Betrag "Waehrung": "Eur"
the Betrag has a row Identifier of "#{row[13]}" which is where it can be found on the excel sheet I am using. I have tried: xml.Betrag "Waehrung": ("Eur"), ("#{row[13]}") with no success, could you please advise?
require 'nokogiri'
builder = Nokogiri::XML::Builder.new do |xml|
xml.Betrag(waehrung: 'EUR') do |e|
e << '150000'
end
end
puts builder.to_xml
=>
<?xml version="1.0"?>
<Betrag waehrung="EUR">150000</Betrag>

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

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'

How can I change this Nokogiri code so that it returns HTML and not the content?

I have this HTML:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE en-note SYSTEM "http://xml.evernote.com/pub/enml2.dtd">
<en-note><div><strong>this is note 2</strong>. it has a url.</div></en-note>
I want to extract this from it:
<div><strong>this is note 2</strong>. it has a url.</div>
At the moment, I am doing this:
html_doc.xpath('//en-note').each do |a_tag|
p a_tag.content
end
Which gets me this:
"this is note 2. it has a url."
How can I change the code so that the actual HTML is returned, not just the text?
Simply use to_s
ruby-1.9.2-p180 :010 > Nokogiri::HTML('<tag>content</tag>').xpath('//tag').first.content
=> "content"
ruby-1.9.2-p180 :004 > Nokogiri::HTML('<tag>content</tag>').xpath('//tag').to_s
=> "<tag>content</tag>"
Just use this
html_doc.at_xpath('//en-note').to_xml

Ruby XML Builder, how to create this namespace?

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>

Resources