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>

In reply to Truncating an HTML node using XPaths in HTML::TreeBuilder::XPath by mldvx4

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.