develooper Front page | perl.libwww | Postings from January 2002

HTML::Element -- printing just one path through tree

From:
Bill Moseley
Date:
January 10, 2002 16:28
Subject:
HTML::Element -- printing just one path through tree
Message ID:
3.0.3.32.20020110162850.025b161c@pop3.hank.org
Hi,

I traverse a parse tree looking for a specific node (it's a converted text
content e.g. ~text).  I then want to print the entire tree, but only the
path that leads to this text segment.

In simpler terms:

  <b>I <em>traverse</em> just <em>a <big>parse</big> tree</em> looking</b>

So I traverse down looking for "parse".  When I find it I want to generate:

      <b><em><big>parse</big></em></b>

The problem is that I want to leave the tree intact.  

I could recursively follow $h->lineage, and remove any content that's not
pointing to the current node (child), but that destroys my tree.  

I can't clone the entire tree, as I'm not sure where I'm starting from.

I guess I could recursively move up the lineage, store each parent's
content list in some new attribute 

    $parent->{content_save} = [$parent->content_list];

grab $root->as_HTML, then work my way back down the tree fixing each node's
content list along the way.

Hum, is there an easier way?

-- 
Bill Moseley
mailto:moseley@hank.org



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About