Skip to content

DocumentFragment#xpath fails to find specific attribute for elements at the root of the fragment #213

Closed
@Phrogz

Description

require 'nokogiri'
html = DATA.read
doc1 = Nokogiri::HTML(html)
doc2 = Nokogiri::HTML::DocumentFragment.parse(html)

ELEMENT_ONLY = ".//h2"
WITH_ID      = ".//h2[@id='foo']"

p doc1.xpath(ELEMENT_ONLY).first['id'],
  doc1.xpath(WITH_ID),
  doc2.xpath(ELEMENT_ONLY).first['id'],
  doc2.xpath(WITH_ID)

#=> "foo"
#=> [#<Nokogiri::XML::Element:0x80a3c168 name="h2" attributes=[#<Nokogiri::XML::Attr:0x80a3bbb8 name="id" value="foo">] children=[#<Nokogiri::XML::Text:0x80a3b288 "Heading 1">]>]
#=> "foo"
#=> []

__END__
<h2 id="foo">Heading 1</h2>

Same problem applies to at_xpath.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions