Skip to content


Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP


Operating on node attributes #666

zzak opened this Issue · 6 comments

3 participants


From the docs: A Nokogiri::XML::Node may be treated similarly to a hash with regard to attributes

I want to prepend a regular hash attribute

my_hash = { 'my_key' => 'bar' }
my_hash['my_key'].insert 0, 'foo'
my_hash #=> {'my_key'=>'foobar'}

This simply cannot be done with nokogiri

doc = Nokogiri::HTML('<img src="bar" />')
doc.xpath("//img").each { |img| img['src'].insert 0, 'foo' }
doc.to_html #=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"\">\n<html><body><img src=\"bar\"></body></html>\n"

You can do this if you get(attribute) first, however

doc.xpath("//img").each { |img| _src = img['src']; img['src'] = _src.insert 0, 'foo' }
doc.to_html #=> "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\" \"\">\n<html><body><img src=\"foobar\"></body></html>\n"


Thanks for asking this question! Your request for assistance using
Nokogiri will not go unanswered!

However, Nokogiri's Github Issues is reserved for reporting bugs or
submitting patches. If you ask your question on the mailing list, Team
Nokogiri promises someone will provide you with an answer in a timely

If you'd like to read up on Team Nokogiri's rationale for this policy,
please go to

Thank you so much for understanding! And thank you for using Nokogiri.


Done, and done. Sorry for the noise.


I've noted this issue in the 2.0 Roadmap, as we're planning on improving the API, and this is a candidate suggestion.


@flavorjones Could you link to the roadmap for 2.0?


Thanks Mike. feels retarded

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.