The Artima Developer Community
Sponsored Link

Java Buzz Forum
Namespace URI Lookup in Ruby

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Michael Cote

Posts: 10306
Nickname: bushwald
Registered: May, 2003

Cote is a programmer in Austin, Texas.
Namespace URI Lookup in Ruby Posted: May 21, 2005 10:21 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Michael Cote.
Original Post: Namespace URI Lookup in Ruby
Feed Title: Cote's Weblog: Coding, Austin, etc.
Feed URL: https://cote.io/feed/
Feed Description: Using Java to get to the ideal state.
Latest Java Buzz Posts
Latest Java Buzz Posts by Michael Cote
Latest Posts From Cote's Weblog: Coding, Austin, etc.

Advertisement

I've been slowing learning Ruby over the past month or so. Tonight I was playing around with using REXML to parse out RSS feeds, and I started getting concerned about getting the correct prefix for namespace'ed elements -- like the dublin core crap.

As far as I know, you can't gurantee that the prefixes used will be the same. So, you can reliably get dublin core dates with something like element.each("//dc:dates"). The dc part might change document per document.

So, I was looking for a way in XPath or Ruby (REXML, I guess) to figure out the correct XPath to use. I didn't find much (though this seemed to indicate how you'd use XPath, but it didn't seem to work for me), so I tried my dumb-hand at writing some actual Ruby code to create a hash of namespace URI's to their prefixes:

    require 'rexml/document'

    xmlresp = File.open("bushwald.rss")
    xml = REXML::Document.new(xmlresp)
    defaultNS = xml.root.namespace('')

    ns = xml.root.namespaces
    prefixes = xml.root.prefixes

    # remove the default, so lists 
    # are equal
    ns.delete(defaultNS)
    nsHash = {defaultNS => ''}
    ns.each_index{|i|
        uri = ns[i] 
        prefix = prefixes[i]
        nsHash[uri] = prefix
        #puts uri+" = "+prefix
    }

    #print it out
    nsHash.each{|key, value|
     puts key+" = "+value
    }

Which, in the case of my del.icio.us feed, outputs this:

mcote-laptop:~/dev/ruby-playgrond cote$ ruby ns-hash.rb
http://www.w3.org/1999/02/22-rdf-syntax-ns# = rdf
http://purl.org/rss/1.0/ = 
http://purl.org/rss/1.0/modules/taxonomy/ = taxo
http://purl.org/dc/elements/1.1/ = dc
http://purl.org/rss/1.0/modules/syndication/ = syn
http://webns.net/mvcb/ = admin

I really have no idea if that's anything, but it sure is fun typing up and running Ruby.

Read: Namespace URI Lookup in Ruby

Topic: Footy Relagation Day Previous Topic   Next Topic Topic: Trading off

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use