Skip to content

Instantly share code, notes, and snippets.

@jrochkind
Created November 29, 2011 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jrochkind/1405370 to your computer and use it in GitHub Desktop.
Save jrochkind/1405370 to your computer and use it in GitHub Desktop.
hpricot vs nokogiri xpaths on intermediate nodes
#!/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")'
@jrochkind
Copy link
Author

1 : hp_parent = Hpricot::XML(xml).at("//parent")

  ==> <parent>
    <child>
      <grandchild>
      </grandchild>
    </child>

    <child>
    </child>
  </parent>


2 : nk_parent = Nokogiri::XML(xml).at("//parent")

  ==> <parent>
    <child>
      <grandchild>
      </grandchild>
    </child>

    <child>
    </child>
  </parent>


3 : hp_parent.at('/child')

  ==> <child>
      <grandchild>
      </grandchild>
    </child>


4 : nk_parent.at('/child')

  ==>


5 : hp_parent.at('/root/parent')

  ==>


6 : nk_parent.at('/root/parent')

  ==> <parent>
    <child>
      <grandchild>
      </grandchild>
    </child>

    <child>
    </child>
  </parent>


7 : hp_parent.at('child')

  ==> <child>
      <grandchild>
      </grandchild>
    </child>


8 : nk_parent.at('child')

  ==> <child>
      <grandchild>
      </grandchild>
    </child>


9 : nk_parent.at('child/grandchild')

  ==> <grandchild>
      </grandchild>


10 : nk_parent.at_xpath('child/grandchild')

  ==> <grandchild>
      </grandchild>


11 : hp_parent.at("./child")

  ==> <parent>
    <child>
      <grandchild>
      </grandchild>
    </child>

    <child>
    </child>
  </parent>


12 : nk_parent.at("./child")

  ==> <child>
      <grandchild>
      </grandchild>
    </child>


13 : hp_parent.at("./child/grandchild")

  ==> <parent>
    <child>
      <grandchild>
      </grandchild>
    </child>

    <child>
    </child>
  </parent>


14 : nk_parent.at("./child/grandchild")

  ==> <grandchild>
      </grandchild>


15 : hp_parent.at("./child/wrong")

  ==> <parent>
    <child>
      <grandchild>
      </grandchild>
    </child>

    <child>
    </child>
  </parent>


16 : nk_parent.at("./child/wrong")

  ==>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment