# Hpricot query

**URL:** <https://www.ruby-forum.com/t/hpricot-query/147122>\
**Category:** Ruby\
**Created:** [August 28, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122 "2008-08-28T16:46:31Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 28, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/1 "2008-08-28T16:46:31Z")

</div>

Hi all, I have a question about Hpricot:

I’m trying to parse an XML document (a google calendar feed), but I  
have problems when I try to get attributes of a tag with a namespace  
(ie: a colon).

Here’s my code for this: date =  
entry.at(‘gd:when’).attributes[‘startTime’]

Instead of having the expected time returned I just get ‘nil’.

Anyone know why this is? Or how to get it to work properly?

Many thanks in advance,

Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 28, 2008, 4:52pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/2 "2008-08-28T16:52:59Z")

</div>

hpricot isn’t quite friendly with namespaces from what I have tried  
before (unsure about the current version). Did you tried doing a full  
xpath query?

@

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 28, 2008, 5:03pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/3 "2008-08-28T17:03:32Z")

</div>

On Aug 28, 3:47 pm, Anthony P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > have problems when I try to get attributes of a tag with a namespace
> 
> > Mark

Hi Anthony, I haven’t I must admit, unfortunately I’m not really  
familiar with xpath and I don’t know how to find the tag I need with  
it, if anybody knows how to do that I’d be very grateful for some  
pointers!

Thanks,

Mark

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 28, 2008, 5:52pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/4 "2008-08-28T17:52:47Z")

</div>

Actually google calander has a ruby plugin/api located here  
[http://googlecalendar.rubyforge.org/plugins/doc/](http://googlecalendar.rubyforge.org/plugins/doc/)

If that’s not your flavor you can start with hpricot’s documentation  
on xpaths here  
[http://code.whytheluckystiff.net/hpricot/wiki/SupportedXpathExpressions](http://code.whytheluckystiff.net/hpricot/wiki/SupportedXpathExpressions)

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 28, 2008, 8:23pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/5 "2008-08-28T20:23:24Z")

</div>

On Thu, Aug 28, 2008 at 11:40:33PM +0900, Mark wrote:

> I’m trying to parse an XML document (a google calendar feed), but I  
> have problems when I try to get attributes of a tag with a namespace  
> (ie: a colon).
> 
> Here’s my code for this: date =  
> entry.at(‘gd:when’).attributes[‘startTime’]

Your query is good. I think it must be something else.

require ‘rubygems’  
require ‘open-uri’  
require ‘hpricot’

url =  
“[http://www.google.com/calendar/feeds/removed\_email\_address@domain.invalid/public/full](http://www.google.com/calendar/feeds/removed_email_address@domain.invalid/public/full)”  
xml = Hpricot.XML(open(url).read)  
puts xml.at(‘gd:when’).attributes[‘startTime’]

\_why

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 29, 2008, 12:32pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/6 "2008-08-29T12:32:42Z")

</div>

On Aug 28, 7:18 pm, \_why [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> require ‘rubygems’  
> require ‘open-uri’  
> require ‘hpricot’
> 
> url = “[http://www.google.com/calendar/feeds/removed\_email\_address@domain.invalid/public/full](http://www.google.com/calendar/feeds/removed_email_address@domain.invalid/public/full)”  
> xml = Hpricot.XML(open(url).read)  
> puts xml.at(‘gd:when’).attributes[‘startTime’]
> 
> \_why

Aha, putting the .XML in the Hpricot call seems to have done the  
trick, thanks!

And thanks for creating such a useful tool!

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [January 20, 2009, 8:37pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/7 "2009-01-20T20:37:29Z")

</div>

\_why wrote:

> require ‘rubygems’  
> require ‘open-uri’  
> require ‘hpricot’
> 
> url =  
> “[http://www.google.com/calendar/feeds/removed\_email\_address@domain.invalid/public/full](http://www.google.com/calendar/feeds/removed_email_address@domain.invalid/public/full)”  
> xml = Hpricot.XML(open(url).read)  
> puts xml.at(‘gd:when’).attributes[‘startTime’]
> 
> \_why

Old thread brought to life…

The .XML() method was the key for me to get namespaces. Although I did  
notice that it still doesn’t quite work as expected.

For example this works:

xml.at(‘media:content’)

But not this:

xml.search(‘media:content/media:category’)

What I ended up having to do to get the desire effect was:

xml.at(‘media:content’).search(‘media:category’)

Is this a bug?

---

<div class="post-metadata">

**Author:** ![mark](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@mark](https://www.ruby-forum.com/u/mark)\
**Post date:** [August 28, 2008, 6:07pm UTC](https://www.ruby-forum.com/t/hpricot-query/147122/8 "2008-08-28T18:07:25Z")

</div>

On Aug 28, 4:45 pm, Anthony P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > entry.at(‘gd:when’).attributes[‘startTime’]  
> > > > familiar with xpath and I don’t know how to find the tag I need with  
> > > > it, if anybody knows how to do that I’d be very grateful for some  
> > > > pointers!
> 
> > Thanks,
> 
> > Mark

Thanks Anthony, unfortunately there are no examples of what I need to  
do and it appears the colon has another meaning in xpath queries.

Thanks for the link to the ruby plugin, I’ll have to look into that  
further.

I’m still very much open to suggestions of how to do it the original  
way, however.
