user1596069
user1596069

Reputation: 65

How to delete a node missing a child with Ruby and Nokogiri

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

Answers (2)

Arup Rakshit
Arup Rakshit

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

pguardiario
pguardiario

Reputation: 54984

You would do:

doc.xpath('//release[not(album)]').remove

Upvotes: 3

Related Questions