I searched for text nodes on a document fragment. This works, as the following snippet shows:
doc = Nokogiri::HTML::DocumentFragment.parse("<p>foo</p>")
doc.xpath('.//text()')
=> [#<Nokogiri::XML::Text:0x3fe56c8c02a8 "foo">]
However, my root node may not have an tag like <p>
, it could be a simple string like "foo"
. Then this query fails.
doc = Nokogiri::HTML::DocumentFragment.parse("foo")
doc.xpath('.//text()')
=> []
Changing the query to doc.xpath('text()')
solves the problem.
Is there a query to combine both behaviour?
foo
is not a tag based on your example. – Conationfoo
? – Conationparse("<root>#{original_xml}</root>")
. The not working example (parse("foo")
) is not valid xml afaik (DocumentFragment). – Palgrave