From: Robert Klemme Date: 2011-10-22T00:45:31+09:00 Subject: Re: NOKOGIRI::XML | Delete parent where child count = 0 On Fri, Oct 21, 2011 at 4:09 AM, Ruby Mania wrote: > All I wanna do is parse the XML below and delete Peter and Sam as they > don't have any children > > {code} > >   >    23 >    230231111 >   >   >   >   >    25 >    222222222 >   >   >   > > {code} Normally you should be able to pull this off with something like this XPath //*[count(./*)=0 and matches(text(), "\A\s*\z")] Apparently this does not work, but you can do this with Nokogiri irb(main):060:0> puts d.xpath('//*[count(./*)=0]').select {|n| /\A\s*\z/ =~ n.text} => nil You can remove with irb(main):067:0> d.xpath('//*[count(./*)=0]').select {|n| /\A\s*\z/ =~ n.text}.each &:remove => [#]>, #]>] irb(main):068:0> puts d 23 230231111 25 222222222 => nil irb(main):069:0> Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/