I have this html markup:
<div class="item"><a href="www"></a></div>
and I'd like to get this:
<div><a></a></div>
How can I do it with Nokogiri?
require 'nokogiri'
doc = Nokogiri::HTML('<div class="item"><a href="www"></a></div>')
You could remove all attributes by xpath
:
doc.xpath('//@*').remove
Or, if you ever need to do something more complex, sometimes it's easier to traverse all elements with:
doc.traverse do |node|
node.keys.each do |attribute|
node.delete attribute
end
end
That works for all but xml namespace attributes (xmlns=). you can easily strip those off too via doc.remove_namespaces! (include the exclamation point, otherwise it won't really remove them)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With