in reply to HTML and Xpath

Maybe an oversight, maybe a concious decision, but since each $result is a HTML::Element, you may need to detach it from the tree, seems to work
#!/usr/bin/perl -- use strict; use warnings; use HTML::TreeBuilder::XPath; my $tree = HTML::TreeBuilder::XPath->new; $tree->parse_content(<<'__HTML__'); <html> <body> <div> <div class="here">this's the value</div> </div> <div> <div class="here">this's the value</div> </div> <div> <div class="here">this's the value</div> </div> <div> <div class="here">this's the value</div> </div> <div> <div class="here">this's the value</div> </div> <div> <div class="here">this's the value</div> </div> </body> </html> __HTML__ for my $result ($tree->findnodes(q{/html/body/div})) { print "$result ", $result->detach(),"\n<br>\n"; print $result->findvalue(q{//div[@class="here"]}); print "\n<br>\n\n"; } __END__ HTML::Element=HASH(0x1b7897c) HTML::Element=HASH(0x1b787e4) <br> this's the value <br> HTML::Element=HASH(0x1b78a48) HTML::Element=HASH(0x1b787e4) <br> this's the value <br> HTML::Element=HASH(0x1b78afc) HTML::Element=HASH(0x1b787e4) <br> this's the value <br> HTML::Element=HASH(0x1b78bb0) HTML::Element=HASH(0x1b787e4) <br> this's the value <br> HTML::Element=HASH(0x1b78c64) HTML::Element=HASH(0x1b787e4) <br> this's the value <br> HTML::Element=HASH(0x1b78d18) HTML::Element=HASH(0x1b787e4) <br> this's the value <br>

Replies are listed 'Best First'.
Re^2: HTML and Xpath
by Anonymous Monk on Nov 06, 2008 at 15:09 UTC
    Instead of detaching (which modifies tree), you could clone
    print $result->clone->findvalue(q{//div[@class="here"]});

      Yes, i test and both method works, "clone" is really a good option too, thank you

Re^2: HTML and Xpath
by Anonymous Monk on Aug 17, 2009 at 11:10 UTC
    Instead of detaching, you could also
    print $result->findvalue(q{./div[@class="here"]});
    or
    print $result->findvalue(q{div[@class="here"]});
Re^2: HTML and Xpath
by way (Sexton) on Nov 06, 2008 at 15:53 UTC

    Thank you very much, as you told, detaching it from the tree works perfectly.

    Thk U again