그런 다음 gphoto
네임 스페이스 노드를 추출하기 위해 각각의 내부 찾습니다 entry
노드를 검색 할 수 있습니다
모든
//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"
# >> }
# >> ]
. 당신이 특정 사람을 원하는 경우에 당신은 당신이 원하는 무엇을 필터링 할 수 있습니다
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를 사용하는 것보다, 각 노드의 네임 스페이스 속성을보고 노코 기리를 묻는 데요.