Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nokogiri strip all attributes

Tags:

ruby

nokogiri

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?

like image 624
Stephan Avatar asked Dec 21 '14 23:12

Stephan


2 Answers

require 'nokogiri'
doc = Nokogiri::HTML('<div class="item"><a href="www"></a></div>')
  1. You could remove all attributes by xpath:

    doc.xpath('//@*').remove
    
  2. 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
    
like image 195
lakesare Avatar answered Sep 28 '22 03:09

lakesare


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)

like image 38
Marc Towersap Avatar answered Sep 28 '22 03:09

Marc Towersap