Created
November 29, 2011 16:14
-
-
Save jrochkind/1405370 to your computer and use it in GitHub Desktop.
hpricot vs nokogiri xpaths on intermediate nodes
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'nokogiri' | |
require 'hpricot' | |
puts "\n\n" | |
$global_binding = binding() | |
$global_stmt_count = 0 | |
def print_eval(str) | |
$global_stmt_count += 1 | |
val = eval(str, $global_binding) | |
puts "#{$global_stmt_count} : #{str}" | |
puts "\n" | |
puts " ==> #{val}\n\n\n" | |
end | |
xml = <<EOS | |
<root> | |
<parent> | |
<child> | |
<grandchild> | |
</grandchild> | |
</child> | |
<child> | |
</child> | |
</parent> | |
<parent> | |
</parent> | |
</root> | |
EOS | |
print_eval 'hp_parent = Hpricot::XML(xml).at("//parent")' | |
print_eval 'nk_parent = Nokogiri::XML(xml).at("//parent")' | |
print_eval "hp_parent.at('/child')" | |
print_eval "nk_parent.at('/child')" | |
print_eval "hp_parent.at('/root/parent')" | |
print_eval "nk_parent.at('/root/parent')" | |
print_eval "hp_parent.at('child')" | |
print_eval "nk_parent.at('child')" | |
print_eval "nk_parent.at('child/grandchild')" | |
print_eval "nk_parent.at_xpath('child/grandchild')" | |
print_eval 'hp_parent.at("./child")' | |
print_eval 'nk_parent.at("./child")' | |
print_eval 'hp_parent.at("./child/grandchild")' | |
print_eval 'nk_parent.at("./child/grandchild")' | |
print_eval 'hp_parent.at("./child/wrong")' | |
print_eval 'nk_parent.at("./child/wrong")' |
Author
jrochkind
commented
Nov 29, 2011
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment