Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
One line Xpath expression to create a nodeset
songtypes = ['Cover', 'Original', 'Duet', 'Group']
songtypes.each do |song|
node_xml = doc.xpath('Container/Set/*[contains(name(), "Cover")]')
#I wanted to be able to do the following
#
FavoriteCover = node_xml.xpath('./*[contains(name(), "Recommended")]')
RegularCover = node_xml.xpath('./*[not(contains(name(), "Recommended"))]')
#or
FavoriteCover = node_xml.xpath('*[contains(name(), "Recommended")]')
RegularCover = node_xml.xpath('*[not(contains(name(), "Recommended"))]')
#But instead I had to resort to a Rails solution
RegularCover = node_xml.find{ |node| node.name !~ /Recommended/ }
FavoriteCover = node_xml.find{ |node| node.name =~ /Recommended/ }
#Do something with the songs here
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.