7stud2
(7stud --)
22 November 2013 20:34
1
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>
<wantToDelete>
<startDate>2013-11-15</startDate>
</wantToDelete>
</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>
# >>
# >> <startDate>2013-11-15</startDate>
# >>
# >> </random_stuff>
Expected output is :
# >> <?xml version="1.0"?>
# >> <random_stuff>
# >> <startDate>2013-11-15</startDate>
# >> </random_stuff>
···
--
Posted via http://www.ruby-forum.com/ .
abinoam
(Abinoam Praxedes Marques Jr.)
30 November 2013 13:41
2
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
Modifying an HTML/XML document - Nokogiri
it says that "The simplest method of moving a node is assign its parent:"
So, we could...
require 'nokogiri'
doc_str = <<-eot
<random_stuff>
<wantToDelete>
<startDate>2013-11-15</startDate>
</wantToDelete>
</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.
* Question about Nokogiri xpath - Ruby - Ruby-Forum
- It has a tip to look at http://groups.google.com/group/nokogiri-talk
Abinoam Jr.
···
On Fri, Nov 22, 2013 at 5:34 PM, Love U Ruby <lists@ruby-forum.com> wrote:
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>
<wantToDelete>
<startDate>2013-11-15</startDate>
</wantToDelete>
</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>
# >>
# >> <startDate>2013-11-15</startDate>
# >>
# >> </random_stuff>
Expected output is :
# >> <?xml version="1.0"?>
# >> <random_stuff>
# >> <startDate>2013-11-15</startDate>
# >> </random_stuff>
--
Posted via http://www.ruby-forum.com/\ .
7stud2
(7stud --)
30 November 2013 14:22
3
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 Thank you very much...
require 'nokogiri'
doc_str = <<-eot
<random_stuff>
<wantToDelete>
<startDate>2013-11-15</startDate>
</wantToDelete>
</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>
# >> <startDate>2013-11-15</startDate>
# >> </random_stuff>
···
--
Posted via http://www.ruby-forum.com/\ .