# Which behaves correctly, Hpricot or Nokogiri?

**URL:** <https://www.ruby-forum.com/t/which-behaves-correctly-hpricot-or-nokogiri/159197>\
**Category:** Ruby\
**Created:** [February 2, 2009, 11:03am UTC](https://www.ruby-forum.com/t/which-behaves-correctly-hpricot-or-nokogiri/159197 "2009-02-02T11:03:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [February 2, 2009, 11:03am UTC](https://www.ruby-forum.com/t/which-behaves-correctly-hpricot-or-nokogiri/159197/1 "2009-02-02T11:03:24Z")

</div>

I’ve been considering switching to Nokogiri instead of Hpricot, mostly  
'cause Mechanize has switched. However, the two actually behave quite  
differently. The Nokogiri objects don’t simulate standard container  
behavior nearly as well as Hpricot. I also noticed that this:

require ‘nokogiri’  
require ‘hpricot’

xml = ‘content’

doc = Nokogiri::XML(xml)  
puts doc.search(‘first’)[0].attributes[‘look’]  
doc = Hpricot(xml)  
puts doc.search(‘first’)[0].attributes[‘look’]

```
...produces this output:

```

Big & small…  
Big & smal…

```
I don't know which output is the correct one. Does anyone know 

```

what’s  
going on here?  
Thank you…

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [February 2, 2009, 1:52pm UTC](https://www.ruby-forum.com/t/which-behaves-correctly-hpricot-or-nokogiri/159197/2 "2009-02-02T13:52:38Z")

</div>

On Mon, Feb 2, 2009 at 11:00 AM, Just Another Victim of the Ambient  
Morality [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> xml = ‘content’

It’s not valid xml. It should be “Big & small…”  
I guess that for non-valid xml there is now “valid” behavior. Ask  
hpricot and nokogiri developers what happen when xml is not valid  
(they try to fix it or smth?)

> Big & small…  
> Big & smal…

Strange. I get:  
Big small…  
Big & small…

The difference is about ‘&’ which is not valid in xml (& should be  
used instead).

–  
Pozdrawiam

Rados³aw Bu³at  
[http://radarek.jogger.pl](http://radarek.jogger.pl) - mój blog

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [February 2, 2009, 8:16pm UTC](https://www.ruby-forum.com/t/which-behaves-correctly-hpricot-or-nokogiri/159197/3 "2009-02-02T20:16:25Z")

</div>

“Rados³aw Bu³at” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote in message  
news:removed\_email\_address@domain.invalid…

> It’s not valid xml. It should be “Big & small…”  
> I guess that for non-valid xml there is now “valid” behavior. Ask  
> hpricot and nokogiri developers what happen when xml is not valid  
> (they try to fix it or smth?)

```
Actually, "Big &amp; small" is what I wrote in the example. The 

```

second  
output is erroneously missing an “l” but I think that’s understood…  
I’m wondering if anyone knows what the correct behaviour is supposed  
to  
be…

```
Oh, I get it. Maybe my use of & amp ; was translated in whatever 

```

client  
you’re using?

---

<div class="post-metadata">

**Author:** ![maz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e9c0ed/32.png) [@maz](https://www.ruby-forum.com/u/maz)\
**Post date:** [February 2, 2009, 9:01pm UTC](https://www.ruby-forum.com/t/which-behaves-correctly-hpricot-or-nokogiri/159197/4 "2009-02-02T21:01:14Z")

</div>

Just Another Victim of the Ambient M. wrote:

> ```
> I don't know which output is the correct one. Does anyone know what's 
> 
> ```
> 
> going on here?

The second one is correct, because & is an encoding, and an XML tool  
should  
use & outside its interface and & inside its interface.

Now try these XPaths in Hpricot and NokoGiri - which combinations find  
the node?

first[@look = ‘Big & small…’]  
first[@look = ‘Big & small…’]
