Reputation: 65
I'd like to remove nodes that lack a specific child.
For example, I would like to delete the second <release>
node here:
<release>
<artist>Johnny Cash</artist>
<album>Live from Folsom Prison</album>
</release>
<release>
<artist>Johnny Cash</artist>
</release>
I have tried: doc.xpath("//Release[album='']").remove
, but that clearly doesn't work because I need to find the nodes where album doesn't exist and not where it's empty.
The alternative I would need would be to return something like "empty" for any node where album was not included when I run this command albums= doc.search('release/album').map{ |t| t.text}
.
Upvotes: 3
Views: 218
Reputation: 118261
doc = Nokogiri::HTML::DocumentFragment.parse(html)
node_set = doc.search('release')
del_node = node_set.select { |n| n.last_element_child.name != 'album'}.first
node_set.delete(del_node)
or
doc = Nokogiri::HTML(html)
doc.xpath('//release').delete(doc.xpath('//release').last)
Upvotes: -1