mldvx4 has asked for the wisdom of the Perl Monks concerning the following question:
I have some paragraph nodes I'd like to truncate starting with the <br /> element within it. Given the code below at the bottom of the post, I'd like to find a way to just have it print, what's in a paragraph but then leave out or delete everything after a break.
foo01 foo02 foo03
I could find the break itself with '//div/p/br[1]' but then how would I be able to have the script delete that and everything else following it within the parent paragraph element?
for my $d ($root->findnodes($xpath)) { for my $dd ($d->findnodes('something')) { $dd->delete; } print $d->as_trimmed_text,qq(\n) if (defined($d->as_text)); }
Here is the script so far, with data.
#!/usr/bin/perl use HTML::TreeBuilder::XPath; use strict; use warnings; my $root = HTML::TreeBuilder::XPath->new; $root->parse_file(\*DATA) or die("Could not parse the data: $!\n"); $root->eof(); my $xpath = '//div/p'; for my $d ($root->findnodes($xpath)) { print $d->as_trimmed_text,qq(\n) if (defined($d->as_text)); } $root->delete; exit(0); __DATA__ <div><p>foo01<br />bar01</p></div> <div> <p> foo02 <br /> bar02 </p> </div> <div> <p> foo03 <br /> bar03 <br / baz03 </p> </div>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath
by Anonymous Monk on Sep 27, 2019 at 08:35 UTC | |
by mldvx4 (Hermit) on Sep 27, 2019 at 16:06 UTC | |
by poj (Abbot) on Sep 27, 2019 at 16:09 UTC | |
by mldvx4 (Hermit) on Sep 27, 2019 at 18:33 UTC | |
by tangent (Parson) on Sep 28, 2019 at 19:20 UTC | |
by Anonymous Monk on Sep 29, 2019 at 03:55 UTC |