您可以搜索 entry 节点,然后查看每个节点以提取 gphoto 命名空间节点:
require 'nokogiri'
doc = Nokogiri::XML(open('./test.xml'))
hashes = doc.search('//xmlns:entry').map do |entry|
h = {}
entry.search("*[namespace-uri()='http://schemas.google.com/photos/2007']").each do |gphoto|
h[gphoto.name] = gphoto.text
end
h
end
require 'ap'
ap hashes
# >> [
# >> [0] {
# >> "id" => "5582695833628950881",
# >> "name" => "Melody19Months",
# >> "location" => "",
# >> "access" => "public",
# >> "timestamp" => "1299649559000",
# >> "numphotos" => "37",
# >> "user" => "soopingsaw",
# >> "nickname" => "sooping",
# >> "commentingEnabled" => "true",
# >> "commentCount" => "0"
# >> }
# >> ]
这将返回所有 //entry/gphoto:* 注释。如果你只想要某些,你可以过滤你想要的:
require 'nokogiri'
doc = Nokogiri::XML(open('./test.xml'))
hashes = doc.search('//xmlns:entry').map do |entry|
h = {}
entry.search("*[namespace-uri()='http://schemas.google.com/photos/2007']").each do |gphoto|
h[gphoto.name] = gphoto.text if (%w[id thumbnail name numphotos].include?(gphoto.name))
end
h
end
require 'ap'
ap hashes
# >> [
# >> [0] {
# >> "id" => "5582695833628950881",
# >> "name" => "Melody19Months",
# >> "numphotos" => "37"
# >> }
# >> ]
请注意,在原始问题中出现了访问 gphoto:thumbnail 的尝试,但是 //element/gphoto:thumbnails 没有匹配的节点,因此无法找到。
使用命名空间编写搜索的另一种方法是:
require 'nokogiri'
doc = Nokogiri::XML(open('./test.xml'))
hashes = doc.search('//xmlns:entry').map do |entry|
h = {}
entry.search("*").each do |gphoto|
h[gphoto.name] = gphoto.text if (
(gphoto.namespace.prefix=='gphoto') &&
(%w[id thumbnail name numphotos].include?(gphoto.name))
)
end
h
end
require 'ap'
ap hashes
# >> [
# >> [0] {
# >> "id" => "5582695833628950881",
# >> "name" => "Melody19Months",
# >> "numphotos" => "37"
# >> }
# >> ]
它不是使用 XPath,而是要求 Nokogiri 查看每个节点的命名空间属性。