# Hpricot question

**URL:** <https://rubytalk.org/t/hpricot-question/39228>\
**Category:** ruby-talk\
**Created:** [19 July 2007 06:32 UTC](https://rubytalk.org/t/hpricot-question/39228 "2007-07-19T06:32:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ivor\_Paul](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@Ivor\_Paul](https://rubytalk.org/u/Ivor_Paul)\
**Post date:** [19 July 2007 06:32 UTC](https://rubytalk.org/t/hpricot-question/39228/1 "2007-07-19T06:32:15Z")

</div>

I'm a total hpricot newbie, so this is only out of curiousity...

Is the following expected behaviour?  
I found this while struggling through a nasty xml parsing bug.

hstr = Hpricot('\<a/\>\<b\>c\</b\>').to\_s #or to\_html

returns =\> '\<a\>\<b\>c\</b\>\</a\>'

I have found that using to\_original\_html returns '\<a/\>\<b\>c\</b\>' as expected  
but I was wondering why you would want \<a/\> to wrap the other contents when  
it obviously isn't correct? Is this handy behaviour for scrapping?

regards

---

<div class="post-metadata">

**Author:** ![James\_Britt](https://avatars.discourse-cdn.com/v4/letter/j/22d042/32.png) [@James\_Britt](https://rubytalk.org/u/James_Britt)\
**Post date:** [19 July 2007 12:29 UTC](https://rubytalk.org/t/hpricot-question/39228/2 "2007-07-19T12:29:40Z")

</div>

Hmm... if you want modern markup, it is correct.  
\<a href="some\_uri" title="some\_title"\>Link's text here\</a\>  
You'd do well to avoid \<b\>\</b\> in favor of \<strong\>\</strong\>

> **···**
>
> On Jul 19, 2007, at 1:32 AM, Ivor Paul wrote:
> 
> > I'm a total hpricot newbie, so this is only out of curiousity...
> > 
> > Is the following expected behaviour?  
> > I found this while struggling through a nasty xml parsing bug.
> > 
> > hstr = Hpricot('\<a/\>\<b\>c\</b\>').to\_s #or to\_html
> > 
> > returns =\> '\<a\>\<b\>c\</b\>\</a\>'
> > 
> > I have found that using to\_original\_html returns '\<a/\>\<b\>c\</b\>' as expected  
> > but I was wondering why you would want \<a/\> to wrap the other contents when  
> > it obviously isn't correct? Is this handy behaviour for scrapping?
> > 
> > regards

---

<div class="post-metadata">

**Author:** ![Peter\_Cooper](https://avatars.discourse-cdn.com/v4/letter/p/db5fbb/32.png) [@Peter\_Cooper](https://rubytalk.org/u/Peter_Cooper)\
**Post date:** [19 July 2007 13:32 UTC](https://rubytalk.org/t/hpricot-question/39228/3 "2007-07-19T13:32:37Z")

</div>

Yeah, I never understand that either. But it's easily fixed:

h = Hpricot('\<a/\>\<b\>c\</b\>', :xml =\> true)  
h.to\_s =\> "\<a\>\</a\>\<b\>c\</b\>"

I think it's because when you don't define :xml as true, then Hpricot  
acts as an \*HTML\* processor, and therefore tries to correct bad HTML..  
and somehow ends up with the badness of wrapping the empty element.

Cheers,  
Peter Cooper

> **[Ruby Inside: The Ruby Blog](http://www.rubyinside.com/)**
>
> The biggest Ruby and Rails blog since 2006 - tutorials, news, and lots more

> **···**
>
> On 7/19/07, Ivor Paul \<ivorpaul@gmail.com\> wrote:
> 
> > I'm a total hpricot newbie, so this is only out of curiousity...
> > 
> > Is the following expected behaviour?  
> > I found this while struggling through a nasty xml parsing bug.
> > 
> > hstr = Hpricot('\<a/\>\<b\>c\</b\>').to\_s #or to\_html
> > 
> > returns =\> '\<a\>\<b\>c\</b\>\</a\>'
> > 
> > I have found that using to\_original\_html returns '\<a/\>\<b\>c\</b\>' as expected  
> > but I was wondering why you would want \<a/\> to wrap the other contents when  
> > it obviously isn't correct? Is this handy behaviour for scrapping?

---

<div class="post-metadata">

**Author:** ![Ivor\_Paul](https://avatars.discourse-cdn.com/v4/letter/i/ecae2f/32.png) [@Ivor\_Paul](https://rubytalk.org/u/Ivor_Paul)\
**Post date:** [19 July 2007 18:27 UTC](https://rubytalk.org/t/hpricot-question/39228/4 "2007-07-19T18:27:49Z")

</div>

Thanks peter - that is what I wanted to hear! I knew there was some reason  
like that.

> **···**
>
> On 7/19/07, Peter Cooper \<peter@peterc.org\> wrote:
> 
> > On 7/19/07, Ivor Paul \<ivorpaul@gmail.com\> wrote:  
> > \> I'm a total hpricot newbie, so this is only out of curiousity...  
> > \>  
> > \> Is the following expected behaviour?  
> > \> I found this while struggling through a nasty xml parsing bug.  
> > \>  
> > \> hstr = Hpricot('\<a/\>\<b\>c\</b\>').to\_s #or to\_html  
> > \>  
> > \> returns =\> '\<a\>\<b\>c\</b\>\</a\>'  
> > \>  
> > \> I have found that using to\_original\_html returns '\<a/\>\<b\>c\</b\>' as  
> > expected  
> > \> but I was wondering why you would want \<a/\> to wrap the other contents  
> > when  
> > \> it obviously isn't correct? Is this handy behaviour for scrapping?
> > 
> > Yeah, I never understand that either. But it's easily fixed:
> > 
> > h = Hpricot('\<a/\>\<b\>c\</b\>', :xml =\> true)  
> > h.to\_s =\> "\<a\>\</a\>\<b\>c\</b\>"
> > 
> > I think it's because when you don't define :xml as true, then Hpricot  
> > acts as an \*HTML\* processor, and therefore tries to correct bad HTML..  
> > and somehow ends up with the badness of wrapping the empty element.
> > 
> > Cheers,  
> > Peter Cooper  
> > [http://www.rubyinside.com/](http://www.rubyinside.com/)
