rails atom_feed can hide id, published, updated..? - ruby-on-rails

i want feed(text/html) entry appear except id, published, and tag cocat (g:)
this is My atom.builder
atom_feed ({:id => '', 'xmlns:g' => "http://base.google.com/ns/1.0"}) do |feed|
feed.title #brand.name
#products.each do |product|
url = ''
feed.entry product, url:url do |entry|
entry.name(product.name)
end
end
end
this is result
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
<id></id>
<title>Default Shop</title>
<entry>
<id></id>
<published>2020-03-04T05:36:52+09:00</published>
<updated>2021-11-24T10:16:54+09:00</updated>
<link rel="alternate" type="text/html" href="localhost/1/1">
<name>product no</name>
</entry>
</feed>
and i want
<feed xml:lang="en-US" xmlns="http://www.w3.org/2005/Atom" xmlns:g="http://base.google.com/ns/1.0">
<title>Default Shop</title>
<entry>
<g:name>product no</name>
</entry>
</feed>
how can i get this result?

Related

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

Rails: How can I parse an RSS file with Feedjira

I'm trying to parse an RSS file. It throws a 404 response when I do:
Feedjira::Feed.fetch_and_parse url
So I thought trying:
Feedjira::Parser::RSS.parse url
But that returns 0 entries
This is exactly what I'm trying:
<% Feedjira::Parser::RSS.parse("my url here").entries.each do |e| %>
<%= "#{e.title}" %><br />
<% end %>
The rss file is being parse with no problem with php in another web. This is the structure:
<rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
<channel>
<title>Title here</title>
... etc ...
<item>
<title>Title here</title>
... etc ...
</item>
<item> ... etc ... </item>
</channel>
</rss>
It's weird that Feedjira::Feed.fetch_and_parse url wasn't working for you. It's working perfectly fine for me.
Here's how I did it. I'm using Railscasts RSS feed for example purpose:
url = "http://railscasts.com/subscriptions/rZNfSBu1hH7hOwV1mjqyLw/episodes.rss"
feed = Feedjira::Feed.fetch_and_parse url
feed.entries.each do |entry|
puts entry.title
end

XML as an value of html input attribute using XSLT

I have an XML like below.
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE myxml SYSTEM "xyz.dtd">
<myxml payloadID="__PAYLOADID__" timestamp="__TIMESTAMP__" xml:lang="en-US">
<Header>
<From>
<Credential domain="test">
<Identity>blahblah&test</Identity>
</Credential>
</From>
</Header>
<Extrinsic name="GroupLineID"/>
</myxml>
I am trying to create HTML page where above XML need to be placed as value of attribute of html Input tag. I am using Java with Saxon to run XSLT. My XSLT is
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:saxon="http://saxon.sf.net/">
<xsl:output name="test" method="xml" encoding="UTF-8" omit-xml-declaration="yes" indent="yes" />
<xsl:template match="/">
<html >
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<xsl:element name="INPUT">
<xsl:attribute name="NAME">xml</xsl:attribute>
<xsl:attribute name="TYPE">HIDDEN</xsl:attribute>
<xsl:attribute name="VALUE"><xsl:copy-of select="saxon:serialize(myxml,'test')"></xsl:copy-of></xsl:attribute>
</xsl:element>
</html>
</xsl:template>
Output of this XSLT is
<html xmlns:saxon="http://saxon.sf.net/">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<INPUT NAME="xml" TYPE="HIDDEN" VALUE="<myxml payloadID="__PAYLOADID__" timestamp="__TIMESTAMP__" xml:lang="en-US">
<Header>
<From>
<Credential domain="test">
<Identity>blahblah&amp;test</Identity>
</Credential>
</From>
</Header>
<Extrinsic name="GroupLineID"/>
</myxml>">
</html>
But i would need to have xml and doctype declaraction also in output like below
<html xmlns:saxon="http://saxon.sf.net/">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<INPUT NAME="xml" TYPE="HIDDEN" VALUE="<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE myxml SYSTEM "xyz.dtd&quote;><myxml payloadID="__PAYLOADID__" timestamp="__TIMESTAMP__" xml:lang="en-US">
<Header>
<From>
<Credential domain="test">
<Identity>blahblah&amp;test</Identity>
</Credential>
</From>
</Header>
<Extrinsic name="GroupLineID"/>
</myxml>">
</html>
Any help appreciated!
Neither the XML declaration nor the DOCTYPE declaration are part of the XSLT data model so the XSLT processor does not have access to those parts of an XML input document. Using http://andrewjwelch.com/lexev/ you might be able to get the DOCTYPE however.
If you want the XML declaration included, why have you said omit-xml-declaration="yes" in the xsl:output declaration?
Similarly, you can get a DOCTYPE declaration in the output of saxon:serialize if you use the doctype-system and doctype-public attributes of xsl:output.

Rails XML Feed: ID as node attribute

I set up a simple XML feed for a vendor we're using (who refuses to read JSON).
<recipes type="array">
<recipe>
<id type="integer">1</id>
<name>
Hamburgers
</name>
<producturl>
http://test.com
</producturl>
...
</recipe>
...
<recipe>
However, the vendor requests that instead of having an id node, id is an attribute in the parent node. e.g.
<recipes type="array">
<recipe id="1">
<name>
Hamburgers
</name>
<producturl>
http://test.com
</producturl>
...
</recipe>
...
<recipe>
I'm building this with (basically)
xml_feed = []
recipes.each do |recipe|
xml_feed <<{id: recipe.id, name: recipe.name, ...}
end
...
render xml: xml_feed.to_xml(root: 'recipes')
But I'm unsure of how to include the id (or any field) as an attribute in the parent node like that. I googled around and couldn't find anything, nor were the http://api.rubyonrails.org/classes/ActiveRecord/Serialization.html docs very helpful
Thanks!
I would suggest you use the nokogiri gem. It provides all you can possible need for handling XML.
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
xml.objects {
xml.object.classy.thing!
}
}
end
puts builder.to_xml
<?xml version="1.0"?>
<root>
<objects>
<object class="classy" id="thing"/>
</objects>
</root>
The suggestion to use Nokogiri is fine. Just the sintax should be a little bit different to achive what you have requested:
builder = Nokogiri::XML::Builder.new do |xml|
xml.root {
xml.object('type' => 'Client') {
xml.name 'John'
}
}
end
puts builder.to_xml
<?xml version="1.0"?>
<root>
<object type="Client">
<name>John</name>
</object>
</root>

Getting the XML ID using REXML

I have an XML response as included below. How can I obtain the element?
In rails I have:
xml = REXML::Document.new(contacts_as_xml)
Rails.logger.info xml[1].attributes["id"]
Which is not returning 'user#company.com'
Thanks
<?xml version='1.0' encoding='UTF-8'?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:openSearch="http://a9.com/-/spec/opensearch/1.1/" xmlns:gContact="http://schemas.google.com/contact/2008" xmlns:batch="http://schemas.google.com/gdata/batch" xmlns:gd="http://schemas.google.com/g/2005" gd:etag="W/"Ck8BQ38yfyt7I2A9WhNSFk8."">
<id>user#company.com</id>
<updated>2012-10-30T18:14:12.197Z</updated>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<title>Users's Contacts</title>
<link rel="alternate" type="text/html" href="http://www.google.com/" />
<link rel="http://schemas.google.com/g/2005#feed" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full" />
<link rel="http://schemas.google.com/g/2005#post" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full" />
<link rel="http://schemas.google.com/g/2005#batch" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/batch" />
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full?max-results=10000" />
<author>
<name>User Howdy</name>
<email>user#company.com</email>
</author>
<generator version="1.0" uri="http://www.google.com/m8/feeds">Contacts</generator>
<openSearch:totalResults>2</openSearch:totalResults>
<openSearch:startIndex>1</openSearch:startIndex>
<openSearch:itemsPerPage>10000</openSearch:itemsPerPage>
<entry gd:etag=""QXc5eDVSLit7I2A9WhdSFk8PTgU."">
<id>http://www.google.com/m8/feeds/contacts/myuser%40mysite.com/base/970a99881a51f3</id>
<updated>2011-07-25T20:31:50.920Z</updated>
<app:edited xmlns:app="http://www.w3.org/2007/app">2011-07-25T20:31:50.920Z</app:edited>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<title>Jon Paris</title>
<link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/myuser%40mysite.com/970a99881a51f3" />
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/970a99881a51f3" />
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/970a99881a51f3" />
<gd:name>
<gd:fullName>Jon Adams</gd:fullName>
<gd:givenName>Jon</gd:givenName>
<gd:familyName>Adams</gd:familyName>
</gd:name>
<gd:email rel="http://schemas.google.com/g/2005#other" address="jon#adams.com" primary="true" />
<gd:email rel="http://schemas.google.com/g/2005#home" address="jon#adams.com" />
</entry>
<entry gd:etag=""R3YzcTFbKit7I2A9WhJbE00ORQY."">
<id>http://www.google.com/m8/feeds/contacts/myuser%40mysite.com/base/1229b5e8eeef3e3</id>
<updated>2012-09-22T08:57:06.889Z</updated>
<app:edited xmlns:app="http://www.w3.org/2007/app">2012-09-22T08:57:06.889Z</app:edited>
<category scheme="http://schemas.google.com/g/2005#kind" term="http://schemas.google.com/contact/2008#contact" />
<title></title>
<link rel="http://schemas.google.com/contacts/2008/rel#photo" type="image/*" href="https://www.google.com/m8/feeds/photos/media/myuser%40mysite.com/1229b5e8eeef3e3" gd:etag=""UQZOJlcjWit7I2BmGBdVTzBfL2E2GGI5Myk."" />
<link rel="self" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/1229b5e8eeef3e3" />
<link rel="edit" type="application/atom+xml" href="https://www.google.com/m8/feeds/contacts/myuser%40mysite.com/full/1229b5e8eeef3e3" />
<gd:email rel="http://schemas.google.com/g/2005#other" address="person#site.com" primary="true" />
<gd:email rel="http://schemas.google.com/g/2005#home" address="person#gmail.com" />
</entry>
</feed>
Problem
When you do:
xml[1].attributes["id"]
You are getting the text node of <?xml version='1.0' encoding='UTF-8'?> and then trying to get its id attribute. Since it does not exist, you got blank.
Solution
You want:
xml.root.elements['id'].text
#=> user#company.com
Note that "user#company.com" is a text node of the id element. It is not an attribute.

Resources