我想要獲取根元素中的ID,LASTEDITED,EXPIRESS屬性。我正在使用xpath,ruby和nokogiri。但它的工作,任何想法?根節點的xpath屬性
XPATH querys:
doc.xpath('/educationProvider/@id').each do |id_node|
puts node.content
end
doc.xpath('/educationProvider/@lastEdited').each do |lastedited_node|
puts lastedited_node.content
end
doc.xpath('/educationProvider/@expires').each do |expires_node|
puts expires_node.content
end
這是我的XML看起來像:
<?xml version="1.0" encoding="UTF-8"?>
<p:educationProvider xmlns:p="http://skolverket.se/education/provider/1.0" xmlns="http://skolverket.se/education/commontypes/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" expires="2015-01-31" id="provider.uh.msb" lastEdited="2012-11-01T12:51:37" xsi:schemaLocation="http://skolverket.se/education/provider/1.0 educationProvider.xsd">
<p:vCard>
<VERSION/>
<FN/>
<N/>
<ADR>
<LOCALITY>KARLSTAD</LOCALITY>
<PCODE>651 81</PCODE>
</ADR>
<TEL>
<NUMBER>0771-240240</NUMBER>
</TEL>
<EMAIL>
<USERID>[email protected]</USERID>
</EMAIL>
<ORG>
<ORGNAME>Myndigheten för samhällsskydd och beredskap</ORGNAME>
</ORG>
<URL>http://www.msbmyndigheten.se</URL>
</p:vCard>
</p:educationProvider>
這裏是我的RUBY腳本:
require 'rubygems'
require 'nokogiri'
require 'open-uri'
# parse the HTML document with all the links to the XML files.
doc = Nokogiri::HTML(open('http://testnavet.skolverket.se/SusaNavExport/EmilExporter?GetEvent&EMILVersion=1.1&NotExpired&EIAcademicType=UoH&SelectEP'))
# URLS - array
@urls = Array.new
#Get all XML-urls and save them in urls-array
doc.xpath('//a/@href').each do |links|
@urls << links.content
end
@id = Array.new
@lastedited = Array.new
@expires = Array.new
# loop all the url of the XML files
@urls.each do |url|
doc = Nokogiri::HTML(open(url))
# grab the content I want
doc.xpath('/educationProvider/@id').each do |id_node|
id_node.content
end
doc.xpath('/educationProvider/@lastEdited').each do |lastedited_node|
@lastedited << lastedited_node.content
end
doc.xpath('/educationProvider/@expires').each do |expires_node|
@expires << expires_node.content
end
end
#print it out
([email protected] - 1).each do |index|
puts "ID: #{@id[index]}"
puts "Lastedited: #{@lastedited[index]}"
puts "Expiress: #{@expires[index]}"
end
看看這個答案:http://stackoverflow.com/questions/4690737/nokogiri-xpath-namespace-query – biscuit314 2012-02-18 17:20:51