Skip to content

Instantly share code, notes, and snippets.

@advorak
Forked from dtolj/gist:833771
Created February 18, 2011 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save advorak/833794 to your computer and use it in GitHub Desktop.
Save advorak/833794 to your computer and use it in GitHub Desktop.
[myxml]
<item>
<title>Book1</title>
<author>Bob</author>
<link>http://url1.com</link>
</item>
<item>
<title>Book2</title>
<author>Bob</author>
<link>http://url1.com</link>
</item>
doc = Nokogiri::XML(fh)
#insert thumbnail for selected authors.
retval=(doc.xpath("//author[text()='Bob']/..")).collect do |item| # Array#map == Array#collect
media=Nokogiri::XML::Node.new "media:thumbnail", item
media.set_attribute("url","http://url/file.jpg")
author=item.at_xpath "author"
author.add_next_sibling(media)
end
puts doc.to_xml #returns all nodes + Bob's thumbnails
puts retval.to_xml #returns nothing
How to assign the matching block value to an array?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment