> 
> Am I making any mistake?
Sure :).

Some W3C DOM theory:

A document consists of different Nodes - in practice subclasses of Node:
Element, Document, Attribute, Comment, Text, ProcessingInstruction etc
(just from the top of my head - there are some more like
DocumentFragment , CData, ... but it is unlikely you will need them
here). Not every Node has a name, or children, or parent, or xxx. You
have to make sure that the subclass of Node you are talking to is
actually responding to a method you are trying to send him.

a Hpricot DOM is not exactly a W3C DOM, but it is mostly similar:

Only HPricot::Element has children, (not HPricot::Document or
HPricot::Comment or...) and also not every Node has a name - like in
your example HPricot::Document (Similarly HPricot::Text or
HPricot::Comment does not have a name...). Also A HPricot::Document does
not have a parent I think.

Your problem is that you are traversing up, and reach the Document node
which does not have a method name.

So you have to modify your code like this:

if text.parent and text.parent.name and text.parent.name != 'java' and

to

parent = text.parent
if (parent.instance_of? Hpricot::Text) #or with respond_to, or with
parent.parent == nil
  #do the stuff
else
  #you have reached the top Node - Document; nothing to do
end

(The else branch is not needed, I just added it for illustration)

HTH,
Peter

__
http://www.rubyrailways.com