Hi all I want to walk with Nokogiri through the whole DOM of an HTML document and want to do some stuff to the elements; then save the whole DOM again (with the changes applied). I need something like that: dom.each do |tag| if tag.type == "a" # do some stuff to links! if tag.type == "p" # do some stuff to paragraphs if ... end end Sadly I don''t really know how to do this, I''m not very used to XPath and stuff... Thanks Josh -- Posted via http://www.ruby-forum.com/.
Joshua Muheim wrote:> Hi all > > I want to walk with Nokogiri through the whole DOM of an HTML document > and want to do some stuff to the elements; then save the whole DOM again > (with the changes applied). > > I need something like that: > > dom.each do |tag| > if tag.type == "a" > # do some stuff to links! > if tag.type == "p" > # do some stuff to paragraphs > if ... > end > end > > Sadly I don''t really know how to do this, I''m not very used to XPath and > stuff... >Do you really want to do it this way? Why not do an XPath query that selects and processes all <a>, then another one for all <p>?> Thanks > JoshBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
> Do you really want to do it this way? Why not do an XPath query that > selects and processes all <a>, then another one for all <p>?Yes, maybe you''re right, this could work as good. But I''m a bit insecure with the conecpt behind Nogokiri - when I iterate over elements of a DOM and apply changes to them, are they altered within this DOM? I suppose so... And how can I save the DOM back to HTML? Thanks a lot for help Josh -- Posted via http://www.ruby-forum.com/.
Joshua Muheim wrote:>> Do you really want to do it this way? Why not do an XPath query that >> selects and processes all <a>, then another one for all <p>? > > Yes, maybe you''re right, this could work as good. But I''m a bit insecure > with the conecpt behind Nogokiri - when I iterate over elements of a DOM > and apply changes to them, are they altered within this DOM? I suppose > so... And how can I save the DOM back to HTML?I don''t know. Why don''t you read the docs, experiment, and/or ask on the Nokogiri discussion forums?> > Thanks a lot for help > JoshBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.