and I would like to get the following:...">

Nokogiri shares all attributes

I have this html markup:

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

and I would like to get the following:

<div><a></a></div>

How can I do this with Nokogiri?

+4
source share
2 answers
require 'nokogiri'
doc = Nokogiri::HTML('<div class="item"><a href="www"></a></div>')
  • You can remove all attributes with xpath:

    doc.xpath('//@*').remove
    
  • Or, if you ever need to do something more complex, it is sometimes easier to cross all the elements with:

    doc.traverse do |node| 
      node.keys.each do |attribute|
        node.delete attribute
      end
    end
    
+8
source

This works for everyone except the xml namespace attributes (xmlns =). You can also easily remove them using doc.remove_namespaces! (turn on the exclamation mark, otherwise it will not delete them)

0
source

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


All Articles