Search for text nodes in Nokogiri
Asked Answered
E

1

1

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?

Enhanced answered 10/9, 2014 at 7:2 Comment(6)
maybe "//p[text()='foo']" ?Squeegee
foo is not a tag based on your example.Conation
One remark: The string is not fixed, and the outer tag also not. I just want to find all text nodes in all children.Enhanced
you want to find text nodes except foo?Conation
I want to find all text nodes, regardless of its name.Enhanced
A workaround is to wrap it in an additional element parse("<root>#{original_xml}</root>"). The not working example (parse("foo")) is not valid xml afaik (DocumentFragment).Palgrave
S
2

I have not used Nokogiri, but in standard XPath, you should be able to just use the union operator:

doc.xpath('.//text() | text()')
Servility answered 10/9, 2014 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.