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:
<xml version="1.0" encoding="ISO-8859-1"?>
<programmers>
<programmer age="50">Joh'n</programmer>
<programmer age="30">Sally</programmer>
</programmers>
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:
<programmer age="50">John</programmer>
<programmer age="50">Joh'n</programmer>
<programmer age="50">Joh"n</programmer>
The user may type in: John, or Joh'n, or Joh"n.
···
--
Posted via http://www.ruby-forum.com/\.