Skip to content

Instantly share code, notes, and snippets.

@ericchen
Forked from Ameeda/NokogiriXPathNamespaces
Created April 16, 2012 06:59
Show Gist options
  • Save ericchen/2396822 to your computer and use it in GitHub Desktop.
Save ericchen/2396822 to your computer and use it in GitHub Desktop.
Selecting elements in some namespace using Nokogiri with XPath
2 ways to extract elements from within XML namespaces using Nokogiri
When trying to select elemenets in the default namespace, e.g. "xmlns= http://www.w3.org/2005/Atom", try the following two ways. Note the xmlns=" attribute on entry element:
Original xml:
Nokogiri::XML(@xml_string).xpath("//author/name").each do |node|
puts node
end
1. Define a namespace context for your XPath expression and point your XPath steps to match elements in that namespace. Define a namespace-to-prefix mapping and use this prefix (a) in the XPath expression.
xml.xpath("//a:author/a:name", {"a" => "http://www.w3.org/2005/Atom"})
2. LESS FAVORED: Using remove_namespaces! all your elements are under http://schemas.google.com/docs/2007 namespace URI. You must declare the binding bettween this URI an some prefix, say atom, and then the XPath expresion should be /*/atom:author/atom:name
xml = Nokogiri::XML(@xml_string)
xml.remove_namespaces!
xml.xpath("//author/name").each do |node|
puts node.text
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment