-2

This prints 100 WOWs, but not a single WOW in the toString. Why?

for my $node ( $body->findnodes('//a') ) {
    $node->setAttribute( 'href', "WOW" );
}
for my $node ( $body->findnodes('//a') ) {
    print $node->getAttribute('href');
}

print $body->toString(2);
Dan Jacobson
  • 490
  • 3
  • 14
  • What type of object is $body, and what is the data it's accessing. – JGNI Oct 04 '18 at 09:04
  • 5
    You search for `a` elements in the entire document (because you used `//a` instead of `.//a` or `descendant::a`), and it's possible there are 100 `a` elements in the document, but that none that are descendants of `$body`. But we can't tell for sure, because you didn't demonstrate the problem you are having. Please provide a minimal, runnable demonstration of the problem. (See [mcve].) – ikegami Oct 04 '18 at 09:14
  • 3
    Note that preceding your snippet with `use XML::LibXML; my $doc = XML::LibXML->new->parse_string(''); my $body = $doc;` or `use XML::LibXML; my $doc = XML::LibXML->new->parse_string(''); my ($body) = $doc->findnodes('body');` does not yield the output you claim. – ikegami Oct 04 '18 at 09:15

1 Answers1

0

As described in the comment from Ikegami:

You search for a elements in the entire document (because you used //a instead of .//a or descendant::a), and it's possible there are 100 a elements in the document, but that none that are descendants of $body

typing './/a' instead of '//a' is the correct way here.

colidyre
  • 4,170
  • 12
  • 37
  • 53
Dan Jacobson
  • 490
  • 3
  • 14