Forum: Ruby nokogiri xpath query with text containing "&"

Posted by Mert (mert_)
on 2012-08-28 13:04
Hi all,

I am trying to run some xpath queries on a XML file. But it seems
nokogiri can not handle texts containing & values.

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

thanks everyone.
Posted by Robert Klemme (robert_k78)
on 2012-08-28 13:11
(Received via mailing list)
On Tue, Aug 28, 2012 at 1:04 PM, Mert <lists@ruby-forum.com> wrote:
> Hi all,
>
> I am trying to run some xpath queries on a XML file. But it seems
> nokogiri can not handle texts containing &amp; 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 &amp; 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&amp;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 = "<a>foo&amp;bar</a>"
=> "<a>foo&amp;bar</a>"
irb(main):002:0> dom = Nokogiri.XML(xml)
=> #<Nokogiri::XML::Document:0x..fc0107cf6 name="document"
children=[#<Nokogiri::XML::Element:0x..fc0107b34 name="a"
children=[#<Nokogiri::XML::Text:0x..fc010795e "foo&bar">]>]>
irb(main):003:0> dom.xpath '//a[text()="foo&bar"]'
=> [#<Nokogiri::XML::Element:0x..fc0107b34 name="a"
children=[#<Nokogiri::XML::Text:0x..fc010795e "foo&bar">]>]

You tried

irb(main):004:0> dom.xpath '//a[text()="foo&amp;bar"]'
=> []

That does not work because the "&amp;" 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
Posted by Mert (mert_)
on 2012-08-28 13:41
> That's not true.  The issue is with your understanding (or lack
> thereof) of XML Entities.  See below.
>

Thank you very much for your encouragement. I hope you have an
understanding of everything.
Thank you for the answer
Posted by Joel Pearson (virtuoso)
on 2012-08-28 14:33
Mert  wrote in post #1073558:
>> That's not true.  The issue is with your understanding (or lack
>> thereof) of XML Entities.  See below.
>>
>
> Thank you very much for your encouragement. I hope you have an
> understanding of everything.
> Thank you for the answer

Question not the capabilities of Nokogiri or ye shall be judged! :)
Posted by Mert (mert_)
on 2012-08-28 14:39
Joel Pearson wrote in post #1073566:
> Mert  wrote in post #1073558:
>>> That's not true.  The issue is with your understanding (or lack
>>> thereof) of XML Entities.  See below.
>>>
>>
>> Thank you very much for your encouragement. I hope you have an
>> understanding of everything.
>> Thank you for the answer
>
> Question not the capabilities of Nokogiri or ye shall be judged! :)

What are you judging? Such a cruel forum.
Posted by Jan E. (jacques1)
on 2012-08-28 15:10
Mert  wrote in post #1073568:
> What are you judging? Such a cruel forum.

That was a joke. And now stop whining, please.
Posted by Robert Klemme (robert_k78)
on 2012-08-28 15:19
(Received via mailing list)
On Tue, Aug 28, 2012 at 1:41 PM, Mert <lists@ruby-forum.com> wrote:
>> That's not true.  The issue is with your understanding (or lack
>> thereof) of XML Entities.  See below.
>
> Thank you very for your encouragement. I hope you have an understanding
> of everything.

You're right.  That was a bit rude.  I apologize.

Now, back to the factual: I hope you did not stop reading at that
line.  Is your question answered?

Kind regards

robert
Posted by Mert (mert_)
on 2012-08-28 15:31
Robert Klemme wrote in post #1073576:
> On Tue, Aug 28, 2012 at 1:41 PM, Mert <lists@ruby-forum.com> wrote:
>>> That's not true.  The issue is with your understanding (or lack
>>> thereof) of XML Entities.  See below.
>>
>> Thank you very for your encouragement. I hope you have an understanding
>> of everything.
>
> You're right.  That was a bit rude.  I apologize.
>
> Now, back to the factual: I hope you did not stop reading at that
> line.  Is your question answered?
>
> Kind regards
>
> robert

Thank you for the apology. Honestly i could not focus on the rest of it. 
I made a mistake, and asked a question without any wrong intension. I 
would be happy in the afternoon if someone could delete this post.
Posted by Robert Klemme (robert_k78)
on 2012-08-28 16:07
(Received via mailing list)
On Tue, Aug 28, 2012 at 3:31 PM, Mert <lists@ruby-forum.com> wrote:
> Robert Klemme wrote in post #1073576:

>> Now, back to the factual: I hope you did not stop reading at that
>> line.  Is your question answered?

> Thank you for the apology. Honestly i could not focus on the rest of it.
> I made a mistake, and asked a question without any wrong intension.

I am not sure I understand.  You asked a perfectly legal question.
Now, is your question answered?

> I would be happy in the afternoon if someone could delete this post.

That's not possible on a mailing list.

Cheers

robert
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.