# Hpricot query

**URL:** <https://rubytalk.org/t/hpricot-query/48686>\
**Category:** ruby-talk\
**Created:** [28 August 2008 14:40 UTC](https://rubytalk.org/t/hpricot-query/48686 "2008-08-28T14:40:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mark8](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Mark8](https://rubytalk.org/u/Mark8)\
**Post date:** [28 August 2008 14:40 UTC](https://rubytalk.org/t/hpricot-query/48686/1 "2008-08-28T14:40:33Z")

</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](http://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:** ![Anthony\_Pearson](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Anthony\_Pearson](https://rubytalk.org/u/Anthony_Pearson)\
**Post date:** [28 August 2008 14:47 UTC](https://rubytalk.org/t/hpricot-query/48686/2 "2008-08-28T14:47:39Z")

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

@

> **···**
>
> On Thu, Aug 28, 2008 at 9:40 AM, Mark \<markjturner@gmail.com\> wrote:
> 
> > 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:** ![\_why](https://avatars.discourse-cdn.com/v4/letter/_/73ab20/32.png) [@\_why](https://rubytalk.org/u/_why)\
**Post date:** [28 August 2008 18:18 UTC](https://rubytalk.org/t/hpricot-query/48686/3 "2008-08-28T18:18:05Z")

</div>

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

&nbsp;&nbsp;require 'rubygems'  
&nbsp;&nbsp;require 'open-uri'  
&nbsp;&nbsp;require 'hpricot'

&nbsp;&nbsp;url = "[http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full&quot](http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full&quot);  
&nbsp;&nbsp;xml = Hpricot.XML(open(url).read)  
&nbsp;&nbsp;puts xml.at('gd:when').attributes['startTime']

\_why

> **···**
>
> 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']

---

<div class="post-metadata">

**Author:** ![Mark8](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Mark8](https://rubytalk.org/u/Mark8)\
**Post date:** [28 August 2008 14:55 UTC](https://rubytalk.org/t/hpricot-query/48686/4 "2008-08-28T14:55:26Z")

</div>

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

> **···**
>
> On Aug 28, 3:47 pm, Anthony Pearson \<a...@serialtactics.com\> wrote:
> 
> > 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?
> > 
> > @
> > 
> > On Thu, Aug 28, 2008 at 9:40 AM, Mark \<markjtur...@gmail.com\> wrote:  
> > \> 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:** ![Mark8](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Mark8](https://rubytalk.org/u/Mark8)\
**Post date:** [29 August 2008 10:25 UTC](https://rubytalk.org/t/hpricot-query/48686/5 "2008-08-29T10:25:45Z")

</div>

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

And thanks for creating such a useful tool!

> **···**
>
> On Aug 28, 7:18 pm, \_why \<w...@ruby-lang.org\> wrote:
> 
> > 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/developer-calen](http://www.google.com/calendar/feeds/developer-calen)[...@google.com](mailto:...@google.com)/public/full"  
> > xml = Hpricot.XML(open(url).read)  
> > puts xml.at('gd:when').attributes['startTime']
> > 
> > \_why

---

<div class="post-metadata">

**Author:** ![Matthew\_Isleb](https://avatars.discourse-cdn.com/v4/letter/m/edb3f5/32.png) [@Matthew\_Isleb](https://rubytalk.org/u/Matthew_Isleb)\
**Post date:** [20 January 2009 19:35 UTC](https://rubytalk.org/t/hpricot-query/48686/6 "2009-01-20T19:35:54Z")

</div>

\_why wrote:

> &nbsp;&nbsp;require 'rubygems'  
> &nbsp;&nbsp;require 'open-uri'  
> &nbsp;&nbsp;require 'hpricot'
> 
> &nbsp;&nbsp;url =  
> "[http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full&quot](http://www.google.com/calendar/feeds/developer-calendar@google.com/public/full&quot);  
> &nbsp;&nbsp;xml = Hpricot.XML(open(url).read)  
> &nbsp;&nbsp;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?

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/\](http://www.ruby-forum.com/%5C).

---

<div class="post-metadata">

**Author:** ![Anthony\_Pearson](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@Anthony\_Pearson](https://rubytalk.org/u/Anthony_Pearson)\
**Post date:** [28 August 2008 15:45 UTC](https://rubytalk.org/t/hpricot-query/48686/7 "2008-08-28T15:45:14Z")

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

> **···**
>
> On Thu, Aug 28, 2008 at 9:55 AM, Mark \<markjturner@gmail.com\> wrote:
> 
> > On Aug 28, 3:47 pm, Anthony Pearson \<a...@serialtactics.com\> wrote:
> > 
> > > 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?
> > > 
> > > @
> > > 
> > > On Thu, Aug 28, 2008 at 9:40 AM, Mark \<markjtur...@gmail.com\> wrote:  
> > > \> 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
> > 
> > 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:** ![Mark8](https://avatars.discourse-cdn.com/v4/letter/m/a87d85/32.png) [@Mark8](https://rubytalk.org/u/Mark8)\
**Post date:** [28 August 2008 16:00 UTC](https://rubytalk.org/t/hpricot-query/48686/8 "2008-08-28T16:00:26Z")

</div>

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.

> **···**
>
> On Aug 28, 4:45 pm, Anthony Pearson \<a...@serialtactics.com\> wrote:
> 
> > Actually google calander has a ruby plugin/api located herehttp://googlecalendar.rubyforge.org/plugins/doc/
> > 
> > If that's not your flavor you can start with hpricot's documentation  
> > on xpaths herehttp://code.whytheluckystiff.net/hpricot/wiki/SupportedXpathExpressions
> > 
> > On Thu, Aug 28, 2008 at 9:55 AM, Mark \<markjtur...@gmail.com\> wrote:  
> > \> On Aug 28, 3:47 pm, Anthony Pearson \<a...@serialtactics.com\> wrote:  
> > \>\> 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?
> > 
> > \>\> @
> > 
> > \>\> On Thu, Aug 28, 2008 at 9:40 AM, Mark \<markjtur...@gmail.com\> wrote:  
> > \>\> \> 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
> > 
> > \> 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
