[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Hpricot question

Rainer

7/12/2008 12:39:00 PM

Hello,

I'm a humble beginner with Hpricot. I want to write a method that
performs certain actions for every HTML tag of a given HTML fragment.
In the documentation, I have only found search mechanisms that to for
a specific tag, attribute or id. What I need, however is a method that
tells me the first HTML tag of a fragment, whatever it is. For
instance:

a_fragment = "<div id="my_first_div">first div<p>a paragraph</
p><span>a span</span></div>"

Are there methods like "current_tag()" or "current_id()" in Hpricot,
which act like this:

s = current_tag(a_fragment)
puts s
=>
"div"
or
s2 = current_id(a_fragment)
puts s2
=>
"my_first_id"

Any help is greatly appreciated.
1 Answer

Ib Lis

7/12/2008 2:59:00 PM

0

Rainer wrote:
> What I need [...] is a method that
> tells me the first HTML tag of a fragment, whatever it is.

You may want to use Element#name:

$ irb -r hpricot
>> d = Hpricot('<div id="my_first_div">first div<p>a paragraph</p><span>a span</span></div>')
=> #<Hpricot::Doc {elem <div id="my_first_div"> "first div" {elem <p> "a
paragraph</\np>" {elem <span> "a span" </span>}} </div>}>
>> d.search('*').first.name
=> "div"

Regards, Ã?mer [yet another Ruby beginner].



--
Posted via http://www.ruby-....