How can i change the element in XML file?

I am following the example in

irb(main):003:0> doc ="bibliography.xml"))
irb(main):004:0> root = doc.root
irb(main):005:0> root.attributes['id']
  irb(main):006:0> puts root.elements[1].elements["author"]

I can do "root.attributes['id']="XXX""...

irb(main):007:0> puts root.attributes['id']
=> nil

Why cant i do root.elements[2].elements["author"].elements["firstname"]="YYY" ???

I would like to locate one of the elements and reassign the value. How can i do that?

irb(main):002:0> include REXML

irb(main):001:0> require 'rexml/document'