Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

XPath with attribute conditions doesn't find root of HTML fragments

96 views
Skip to first unread message

Martin L.

unread,
Aug 4, 2020, 11:55:11 AM8/4/20
to nokogiri-talk
Hi,

Taking the following HTML fragment...

>> html = "<div id=\"abc\"></div>"
=> "<div id=\"abc\"></div>"

This works:

>> Nokogiri::HTML.fragment(html).at_xpath(".//div")
=> #<Nokogiri::XML::Element:0x2b1a5015da5c name="div" attributes=[#<Nokogiri::XML::Attr:0x2b1a5016e5b4 name="id" value="abc">]>

This gives nil, but should produce the same result as above:

>> Nokogiri::HTML.fragment(html).at_xpath(".//div[@id=\"abc\"]")
=> nil

And the same here:

>> Nokogiri::HTML.fragment(html).at_xpath(".//div[@id]")
=> nil


Best regards
Martin

Mike Dalessio

unread,
Aug 4, 2020, 12:13:05 PM8/4/20
to nokogiri-talk
Hi Martin,

Thanks for asking this question. Yes, you're describing the same issue as #572. A workaround that you may want to use is to begin your xpath queries with "./" when searching on a fragment, or to use no prefix at all. Either of these queries will work for you:

frag.at_xpath("./div[@id = 'abc']")
frag.at_xpath("div[@id = 'abc']")

Sorry for the trouble.

-mike


--
You received this message because you are subscribed to the Google Groups "nokogiri-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to nokogiri-tal...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/nokogiri-talk/879ab061-6396-412f-ab1f-7b6bfa26804ao%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages