删除标记但保留文本

所以我在xml文件中有这个标签

 Something 123 

我想要的结果是使用Nokogiri并完全删除它的标签,因此它不再是可点击的链接,例如

 Something 123 

我的尝试:

 content = Nokogiri::XML.fragment(page_content) content.search('.//a').remove 

但这也删除了文本。

有关如何使用Nokogiri实现我想要的结果的任何建议?

这是我要做的:

 require 'nokogiri' doc = Nokogiri::HTML.parse <<-eot Something 123 eot node = doc.at("a") node.replace(node.text) puts doc.to_html 

产量

   Something 123  

更新

如果我有一个包含链接内容的数组怎么办?

暗示

 require 'nokogiri' doc = Nokogiri::HTML.parse <<-eot foo bar baz eot arr = %w(foo bar baz) nodes = doc.search("a") nodes.each {|node| node.replace(node.content) if arr.include?(node.content) } puts doc.to_html 

产量

   foo bar baz   

解包标记的通用方法是 – node.replace(node.children) ,例如:

 doc = Nokogiri::HTML.fragment('
ABC
') doc.css('div').each { |node| node.replace(node.children) } doc.inner_html #=> "ABC"