Brian Candler
10/21/2004 8:29:00 AM
> irb(main):006:0> XPath.first( xmldoc,
> "/rss/channel/item/title[text()='John's Doe']" ).to_s
I'm no expert in XPath, but that looks like a broken XPath query because of
the three single quotes.
> irb(main):007:0> XPath.first( xmldoc,
> "/rss/channel/item/title[text()='John\'s Doe']" ).to_s
That's identical, as you'll see if you try this:
irb(main):001:0> a="text()='John\'s Doe'"
=> "text()='John's Doe'"
You've not inserted a backslash into the string, you just escaped the quote,
and the escaping was removed. You need two backslashes to insert a single
backslash into the string:
irb(main):002:0> a="text()='John\\'s Doe'"
=> "text()='John\\'s Doe'"
(Despite how it looks, there is only a single backslash in there; it's shown
as two because it's inside a double-quoted string, to make it valid Ruby)
irb(main):003:0> a.each_byte { |c| print c.chr," " }
t e x t ( ) = ' J o h n \ ' s D o e ' => "text()='John\\'s Doe'"
However, I've just had a quick scan through the XPath-1.0 spec, and I don't
think that's how you do it. You can include single quotes inside a
double-quoted string, and vice versa. But probably what you want for the
general case is XML character entities: ' or '
Try passing your string through this before constructing your XPath query:
require 'rexml/text'
a = "John's Doe"
b = REXML::Text::normalize(a)
#=> "John's Doe"
HTH,
Brian.