Created
May 5, 2011 14:48
-
-
Save Ameeda/957172 to your computer and use it in GitHub Desktop.
Selecting elements in some namespace using Nokogiri with XPath
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
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 | |
Thank you :)
…Sent from my iPhone
On May 5, 2011, at 11:39 AM, ***@***.*** wrote:
Wow that's really impressive. I had this problem just a few months ago. Wish I would have seen this Gist then!
##
Reply to this email directly or view it on GitHub:
https://gist.github.com/957172
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Wow that's really impressive. I had this problem just a few months ago. Wish I would have seen this Gist then!