Best way to remove blank lines after removing Nokogiri Node

It may be ridiculous, but I have to ask.

I use Nokogiri to parse XML, remove certain tags, and write to the source file with the results. Using .remove leaves blank lines in XML. I am currently using regex to get rid of blank lines. Is there any built-in Nokogiri method that I should use?

Here is what I have:

 require 'Nokogiri' io_path = "/path/to/metadata.xml" io = File.read(io_path) document = Nokogiri::XML(io) document.xpath('//artwork_files', '//tracks', '//previews').remove # write to file and remove blank lines with a regular expression File.open(io_path, 'w') do |x| x << document.to_s.gsub(/\n\s+\n/, "\n") end 
+7
ruby xml nokogiri
source share
3 answers

There are no built-in methods, but we can add one

 class Nokogiri::XML::Document def remove_empty_lines! self.xpath("//text()").each { |text| text.content = text.content.gsub(/\n(\s*\n)+/,"\n") }; self end end 
+7
source share

Performing a replacement for each node text did not work for me either. The problem is that after deleting nodes, text nodes that have just become contiguous do not merge. When you iterate over text nodes, each of them has only one new line, but now there are several of them in a row.

One pretty dirty solution I found was to repeat the document:

 xml = Nokogiri::XML.parse xml.to_xml 

Now adjacent text nodes will be merged, and you can do regular expressions with them.

But this seems like the best option:

https://github.com/tobym/nokogiri-pretty

+1
source share

Deleted blank lines for me;

 doc.xpath('//text()').find_all {|t| t.to_s.strip == ''}.map(&:remove) 
+1
source share

All Articles