Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath attribute of root node

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>[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>

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
([email protected] - 1).each do |index|
  puts "ID: #{@id[index]}"
  puts "Lastedited: #{@lastedited[index]}"
  puts "Expiress: #{@expires[index]}"
end

1 Answers

I wan to fetch the ID, LASTEDITED, EXPIRESS attributes in the root element.

Just use:

/*/@id

This selects the id attribute of the top element of the XML document.

/*/@lastEdited

This selects the lastEdited attribute of the top element of the XML document.

/*/@expires

This selects the expires attribute of the top element of the XML document.

Alternatively, all these three attributes can be selected with a single XPath expression:

/*/@*[contains('|id|lastEdited|expires|', 
               concat('|', name(), '|')
               )
     ]

XSLT - based verification:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:for-each select=
  "/*/@*[contains('|id|lastEdited|expires|',
                  concat('|', name(), '|')
                  )
         ]">
   <xsl:value-of select=
   "concat('&#xA;',
           name(),
           ' = ',
           .
          )"/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

when this XSLT transformation is applied on the provided XML document:

<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>

the Xpath expression is evaluated and for each of the selected attributes their name and value are output:

expires = 2015-01-31
id = provider.uh.msb
lastEdited = 2012-11-01T12:51:37
like image 162
Dimitre Novatchev Avatar answered Oct 16 '25 02:10

Dimitre Novatchev