From: 7stud -- Date: 2009-03-19T05:09:02+09:00 Subject: Re: REXML get specific element Phlip wrote: > 7stud -- wrote: > >> 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. > > You understand Ruby escaping - just not XPath escaping. There is none. Ah. Ok. > If you > use ' to delimit a string, you cannot use ' inside it. I would love to > be proven > wrong, but I did research this for quite a while. > > Why didn't my $name substitution work? It's in the rdocs... > >> 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"] > > Wouldn't this work? > > target = XPath.match(doc, "//programmer[text() = $name]", :name => > $john) > I don't know what you are trying to do there. What is $john? 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"] --output:-- $ ruby r1test.rb Joh'n "Joh'n" [] r1test.rb:14: undefined method `attributes' for nil:NilClass (NoMethodError) I thought the question you presented was how are you going to be able to handle all these cases in the xml: John Joh'n Joh"n The user may type in: John, or Joh'n, or Joh"n. -- Posted via http://www.ruby-forum.com/.