From: Robert Klemme Date: 2012-08-28T20:10:37+09:00 Subject: Re: nokogiri xpath query with text containing "&" On Tue, Aug 28, 2012 at 1:04 PM, Mert wrote: > Hi all, > > I am trying to run some xpath queries on a XML file. But it seems > nokogiri can not handle texts containing & values. That's not true. The issue is with your understanding (or lack thereof) of XML Entities. See below. > here is an example, second line prints nothing. if i change & to > something else in both xml and xpath expression, i get the values i > expect. > > How can i query xpath expressions with XML excapes. > > file_xml = Nokogiri::XML File.open(file_configurable) > print > file_xml.xpath("//Match[text()='value1&value2']/../Value").text.to_s We would need to see your input but I guess it's something like this: irb(main):001:0> xml = "foo&bar" => "foo&bar" irb(main):002:0> dom = Nokogiri.XML(xml) => #]>]> irb(main):003:0> dom.xpath '//a[text()="foo&bar"]' => [#]>] You tried irb(main):004:0> dom.xpath '//a[text()="foo&bar"]' => [] That does not work because the "&" only exists in the external representation of the XML (the file). The XML Entity is parsed and the document contains the literal "&": irb(main):005:0> dom.root.text.to_s => "foo&bar" irb(main):006:0> dom.root.text => "foo&bar" Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/