From: 7stud -- Date: 2009-03-19T04:23:37+09:00 Subject: Re: REXML get specific element Phlip wrote: >> You are missing the '-s. >> Try >> doc.root.elements["programmer[@name='#{entered_name}']"] > > Then that croaks if the name contains an apostrophe. > > Try (IIRC)... > > XPath.first(doc, '//programmer[ @name = $name ]', :name => > entered_name) That doesn't work for me. And I couldn't figure out the escaping necessary to handle both a name entered with a single quote or a double quote. I thought I understood ruby escaping, but not anymore. Here is the easy case: 1) John Sally require 'rexml/document' include REXML f = File.new('xml4.xml') doc = Document.new(f) input_name = gets input_name = input_name.chomp target = XPath.match(doc, "//programmer[text() = 'John']") puts target[0].attributes["age"] ------- $ ruby r1test.rb John 50 Now suppose the programmer's name is Joh'n Joh'n Sally This is one of my favorite attempts to escape a single quote in the input name: input_name = "Joh'n" p input_name input_name = input_name.gsub("'", "\\'") p input_name --output:-- "Joh'n" "Johnn" Can anyone explain what's going on there? -- Posted via http://www.ruby-forum.com/.