Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath

by mldvx4 (Friar)
on Sep 27, 2019 at 07:56 UTC ( [id://11106778]=perlquestion: print w/replies, xml ) Need Help??

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

      Ok, but foo01, foo02, and foo03 are content, not elements. Thus they cannot be siblings to the <br /> element. That mix and match of types (?) prevents the sibling search from being the solution here. I'm still trying a lot of things but want to stay as much as I can with the objects created by and manipulated through HTML::Element.

        Try

        for my $d ($root->findnodes($xpath)) { my @line = $d->content_list; s/^\s+|\s+$//g for @line; printf "%s\n",$line[0]; }
        poj

        A tip for tips is to look around a tip, both above and below :)

        #!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder::XPath; my $tree= HTML::TreeBuilder::XPath->new_from_content(<<'HTML'); <body> <div><p>foo01<br />bar01</p></div> <div> <p> foo02 <br /> bar02 </p> </div> <div> <p> foo03 <br /> bar03 <br / baz03 </p> </div> HTML # $tree->dump; print $tree->as_HTML( '><&' => " " ), "\n";; for my $br ( $tree->findnodes( '//div/p/br[1]' ) ){ $br->parent->splice_content( $br->pindex, int@{ $br->parent->content_array_ref } ); } # $tree->dump; print $tree->as_HTML( '><&' => " " ), "\n";; __END__ <html> <head> </head> <body> <div> <p>foo01<br />bar01</div> <div> <p> foo02 <br /> bar02 </div> <div> <p> foo03 <br /> bar03 <br </p="&lt;/p" baz03="baz03" /> </div> </body> </html> <html> <head> </head> <body> <div> <p>foo01</div> <div> <p> foo02 </div> <div> <p> foo03 </div> </body> </html>

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11106778]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-24 16:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found