Last active
August 29, 2015 14:22
-
-
Save streetcarmonkey/e5d986a774037e517891 to your computer and use it in GitHub Desktop.
Ruby Nokogiri: returning node sets by sub elements or nested text strings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
xml = '<?xml version="1.0" encoding="UTF-8"?> | |
<bookstore> | |
<book id = "1"> | |
<title lang="en">Harry Potter</title> | |
<price>29.99</price> | |
Harry Potter is great! | |
</book> | |
<book id = "2"> | |
<title lang="en">Learning XML</title> | |
<price>39.95</price> | |
This has nothing to do with Harry Potter | |
</book> | |
</bookstore>' | |
require 'nokogiri' | |
require 'pp' | |
doc = Nokogiri::XML(xml) | |
puts "Should return book id 2 and its title" | |
nodeset1 = doc.xpath("//book[price>35.00]") | |
nodeset1_sub_element = nodeset1.xpath(".//title").text | |
puts nodeset1 | |
puts nodeset1_sub_element | |
puts '' | |
puts "Should return book id 1 and its title" | |
nodeset2 = doc.xpath("//*[text()[normalize-space(.)='Harry Potter is great!']]") | |
nodeset2_sub_element = nodeset2.xpath(".//title").text | |
puts nodeset2 | |
puts nodeset2_sub_element | |
puts '' | |
puts 'Should return book ids 1 and 2 as both have substrings with "Harry Potter"in them' | |
all_nodesets = doc.xpath("//*[text()[contains(.,'Harry Potter')]]") | |
all_nodesets_sub_elements = all_nodesets.xpath(".//title") | |
puts all_nodesets | |
all_nodesets_sub_elements.each { | node | puts node.text } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment