How to remove blank lines using nokogiri?

require ‘nokogiri’

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

doc = Nokogiri::XML.parse <<-eot
<random_stuff>

2013-11-15

</random_stuff>
eot

node = doc.at(‘random_stuff’)
node.replace(Nokogiri::XML::Node.new(node.name,doc)).children =
node.children
node = doc.at(‘wantToDelete’)
node.replace(node.children)
puts doc.remove_empty_lines!.to_xml

>> <?xml version="1.0"?>

>> <random_stuff>

>>

>> 2013-11-15

>>

>> </random_stuff>

Expected output is :

>> <?xml version="1.0"?>

>> <random_stuff>

>> 2013-11-15

>> </random_stuff>

Note: I’m not experienced with Nokogiri… but…

The two xml, the desired (without blanks) and the real one (with
blanks), (for most practical reasons) mean the same in XML.

But, if you really want to indent it…

At
http://blog.slashpoundbang.com/post/1454850669/how-to-pretty-print-xml-with-nokogiri
you can get the result that you want in terms of indentation.

At
http://nokogiri.org/tutorials/modifying_an_html_xml_document.html
it says that “The simplest method of moving a node is assign its
parent:”

So, we could…

require ‘nokogiri’

doc_str = <<-eot
<random_stuff>

2013-11-15

</random_stuff>
eot

doc = Nokogiri.XML(doc_str) do |config|
config.default_xml.noblanks
end

random_stuff_node = doc.at(‘random_stuff’)
want_to_delete_node = doc.at(‘random_stuff/wantToDelete’)
start_date_node = doc.at(‘random_stuff/wantToDelete/startDate’)

start_date_node.parent = random_stuff_node
want_to_delete_node.remove

puts doc.to_xml(:indent=>2)

Was this what you wanted to accomplish?

Any Nokogiri Guru around to “bless” this humble code?

In time, there’s another post in this forum related to this.

Abinoam Jr.

Abinoam Jr. wrote in post #1129039:

Note: I’m not experienced with Nokogiri… but…

The two xml, the desired (without blanks) and the real one (with
blanks), (for most practical reasons) mean the same in XML.

But, if you really want to indent it…

Was this what you wanted to accomplish?

Yes… I am :slight_smile: Thank you very much…

require ‘nokogiri’

doc_str = <<-eot
<random_stuff>

2013-11-15

</random_stuff>
eot

doc = Nokogiri.XML(doc_str) do |config|
config.default_xml.noblanks
end

random_stuff_node = doc.at(‘random_stuff’)
want_to_delete_node = doc.at(‘random_stuff/wantToDelete’)
start_date_node = doc.at(‘random_stuff/wantToDelete/startDate’)

start_date_node.parent = random_stuff_node
want_to_delete_node.remove

puts doc.to_xml(:indent=>2)

>> <?xml version="1.0"?>

>> <random_stuff>

>> 2013-11-15

>> </random_stuff>

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs