Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7519139
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:51:13+00:00 2026-05-30T01:51:13+00:00

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

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T01:51:14+00:00Added an answer on May 30, 2026 at 1:51 am

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

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wan't to do this using Mono, but Mono is so transparent the question
I wan't to get data's from CallLog.Calls.CONTENT_URI in non Activity classes. Am using cursor
i wan to disable drag event using jquery and want to bind one another
I wan to get the value of only one week. I am using the
I wan't to make a string regexp safe in Ruby. I have: comment =
I wan't to use this controll in my app but cant find which it
I wan't to open a pdf file on Xcode using phone gap[corodova 1.7.0]. Is
I wan to select the tiles but able to edit the other and type
I wan't to use some C source in my Objective-c proj but the source
I am parsing an XML but having some problem regrading its attribute. I wan

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.