Remove external tags from an element using Nokogiri?

This is what I want to do:

Remove the span nodes with the class none.

Delete the extra nodes, but keep the text inside them.

Remove all br nodes and replace them with p nodes

<p class="normal"> <span class="none"> <extra>Some text goes here</extra> </span> <span class="none"> <br/> </span> <span class="none"> <extra>Some other text goes here</extra> <br/> </span> </p> 

This is the result that I would like to achieve:

 <p class="normal">Some text goes here</p> <p class="normal">Some other text goes here</p> 

I have tried this so far:

 doc.xpath('html/body/p/span').each do |span| span.attribute_nodes.each do |a| if a.value == "none" span.children.each do |child| span.parent << child end span.remove end end end 

But this is the result that I get, not even in the correct order:

 <p class="normal"><br /><br />Some text goes hereSome other text goes here</p> 
+4
source share
1 answer

try it

 require 'rubygems' require 'nokogiri' doc = Nokogiri::XML(DATA) doc.css("span.none, extra").each do |span| span.swap(span.children) end # via http://stackoverflow.com/questions/8937846/how-do-i-wrap-html-untagged-text-with-p-tag-using-nokogiri doc.search("//br/preceding-sibling::text()|//br/following-sibling::text()").each do |node| if node.content !~ /\A\s*\Z/ node.replace(doc.create_element('p', node)) end end doc.css('br').remove puts doc __END__ <p class="normal"> <span class="none"> <extra>Some text goes here</extra> </span> <span class="none"> <br/> </span> <span class="none"> <extra>Some other text goes here</extra> <br/> </span> </p> 

What seal

 <?xml version="1.0"?> <p class="normal"> <p>Some text goes here</p> <p>Some other text goes here</p> </p> 
+8
source

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


All Articles