Stephan Stephan - 1 year ago 162
Ruby Question

Nokogiri strip all attributes

I have this html markup:

<div class="item"><a href="www"></a></div>

and I'd like to get this:


How can I do it with Nokogiri?

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

  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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download