Nokogiri strip all attributes
Asked Answered
C

2

5

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?

Cribbs answered 21/12, 2014 at 23:58 Comment(2)
What have you written? We can help you better by working from the code you wrote, than for us to start from scratch, write code that has absolutely no relation to what you have, and you have to force our suggestions into place.Protomorphic
@theTinMan, I was trying to do it through xpath, but thanks to my rather restricted knowledge of it I didn't know I could use splat. And I thought there may be special Nokogiri function that I missed in my google searhes, so I decided not to write down my research this time.Cribbs
S
14
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
    
Sinuous answered 22/12, 2014 at 0:0 Comment(0)
U
1

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)

Umbria answered 17/8, 2016 at 23:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.