I have a nested XML document that looks like this:
<?xml version="1.0"?> <phone> <name>test</name> <descr>description</descr> <empty/> <lines> <line>12345</line> <css/> </lines> </phone>
I need to remove all empty XML nodes, for example <empty/> and <css/> .
I got something like:
doc = Nokogiri::XML::DocumentFragment.parse <<-EOXML <phone> <name>test</name> <descr>description</descr> <empty/> <lines> <line>12345</line> <css/> </lines> </phone> EOXML phone = doc.css("phone") phone.children.each do | child | child.remove if child.inner_text == '' end
In the above code, only the first empty tag is removed, for example. <empty/> . I cannot go inside a nested block. I think I need a recursive strategy. I carefully read the Nokogiri documentation and checked many examples, but have not yet found a solution.
How can i fix this?
I am using Ruby 1.9.3 and Nokogiri 1.5.10.
ruby xml recursion nokogiri
pastoreerrante
source share