I wan to fetch the ID, LASTEDITED, EXPIRESS attributes in the root element. I am using xpath, ruby and nokogiri. But it dosent work, any ideas?
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
This how my XML looks like:
<?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>utbildning@msbmyndigheten.se</USERID>
</EMAIL>
<ORG>
<ORGNAME>Myndigheten för samhällsskydd och beredskap</ORGNAME>
</ORG>
<URL>http://www.msbmyndigheten.se</URL>
</p:vCard>
</p:educationProvider>
HERE IS MY RUBY-SCRIPT:
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
(0..@id.length - 1).each do |index|
puts "ID: #{@id[index]}"
puts "Lastedited: #{@lastedited[index]}"
puts "Expiress: #{@expires[index]}"
end
Just use:
This selects the
idattribute of the top element of the XML document.This selects the
lastEditedattribute of the top element of the XML document.This selects the
expiresattribute of the top element of the XML document.Alternatively, all these three attributes can be selected with a single XPath expression:
XSLT – based verification:
when this XSLT transformation is applied on the provided XML document:
the Xpath expression is evaluated and for each of the selected attributes their name and value are output: