Created
February 18, 2011 15:03
-
-
Save dtolj/833771 to your computer and use it in GitHub Desktop.
Nokogiri block assignment
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[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']/..")).each do |item| | |
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