Skip to content

Instantly share code, notes, and snippets.

@advorak
Forked from dtolj/gist:833771
Created February 18, 2011 15:13
Show Gist options
  • Select an option

  • Save advorak/833794 to your computer and use it in GitHub Desktop.

Select an option

Save advorak/833794 to your computer and use it in GitHub Desktop.

Revisions

  1. advorak revised this gist Feb 18, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -13,7 +13,7 @@
    doc = Nokogiri::XML(fh)

    #insert thumbnail for selected authors.
    retval=(doc.xpath("//author[text()='Bob']/..")).each do |item|
    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"
  2. @dtolj dtolj revised this gist Feb 18, 2011. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,8 @@
    <title>Book1</title>
    <author>Bob</author>
    <link>http://url1.com</link>

    </item>
    <item>
    <title>Book2</title>
    <author>Bob</author>
    <link>http://url1.com</link>
  3. @dtolj dtolj created this gist Feb 18, 2011.
    25 changes: 25 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    [myxml]
    <item>
    <title>Book1</title>
    <author>Bob</author>
    <link>http://url1.com</link>

    <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?