Revisions
-
Ameeda created this gist
May 5, 2011 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ 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