Skip to content

Instantly share code, notes, and snippets.

@chensoren
Forked from Ameeda/NokogiriXPathNamespaces
Created April 16, 2012 06:59

Revisions

  1. @Ameeda Ameeda created this gist May 5, 2011.
    19 changes: 19 additions & 0 deletions NokogiriXPathNamespaces
    Original 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