How to get content without nested elements using Nokogiri

src = '<paragraph>And bla foo <note>not important</note> bar baz</paragraph>'
doc = Nokogiri::XML(src)
puts doc.xpath('paragraph').first.content

The return code returns:

"And bla foo not important bar baz"

I am looking for a way to get content without nested elements. The above example is just an XML example, but in this example I want this to be the result:

"And bla foo bar baz"
+3
source share
2 answers
puts doc.xpath('paragraph/child::text()')

I have not used XPath in anger for many years, but it seems to work.

Or better yet:

puts doc.xpath('paragraph/child::text()').to_s.squeeze(' ')
+7
source

You can do something like

doc.xpath('paragraph').children.map { |e| e.text if e.text? }.join

This will return "And bla foo bar baz" from your example

+2
source

Source: https://habr.com/ru/post/1785165/


All Articles