From: Matthias Reitinger Date: 2009-03-19T06:56:01+09:00 Subject: Re: REXML get specific element 7stud -- wrote: > This doesn't work for me: > > > > Joh'n > Sally > > > > require 'rexml/document' > include REXML > > f = File.new('xml4.xml') > doc = Document.new(f) > > input_name = gets > input_name = input_name.chomp > p input_name > > target = XPath.match(doc, "//programmer[text() = $name]", :name => > $input_name) > p target > > puts target[0].attributes["age"] There are several flaws in your code: First, $input_name is not the same variable as input_name. Second, according to the REXML documentation the variables hash for the XPath query is in fact the fourth argument to XPath#match, not the third. The third one is for the namespaces hash. And lastly, you have to use strings as keys here, not symbols (took me some time to figure that one out). So this does the trick: require 'rexml/document' include REXML doc = Document.new(DATA) input_name = gets.chomp p input_name target = XPath.match(doc, "//programmer[text()=$name]", {}, "name" => input_name) p target puts target[0].attributes["age"] __END__ John Joh'n Joh"n -Matthias -- Posted via http://www.ruby-forum.com/.