How to get the absolute node path in XML using XPath and Ruby?
问题 Basically I want to extract the absolute path from a node to root and report it to the console or a file. Below is the current solution: require "rexml/document" include REXML def get_path(xml_doc, key) XPath.each(xml_doc, key) do |node| puts "\"#{node}\"" XPath.each(node, '(ancestor::#node)') do |el| # puts el end end end test_doc = Document.new <<EOF <root> <level1 key="1" value="B"> <level2 key="12" value="B" /> <level2 key="13" value="B" /> </level1> </root> EOF get_path test_doc, "//*/[