Consider this snippet:
my $PRSR = XML::LibXML->new();
my $PP = XML::LibXML::PrettyPrint->new(indent_string => " ");
my $tmRawData = <DATA>;
my $tmDOM = $PRSR->load_xml(string => $tmRawData);
my $hm="SHA-1";
say $tmDOM->findvalue('//checksum[checksumMethod="' . $hm .'"]/checksumValue');
my $prettyxml = $PP->pretty_print($tmDOM->documentElement())->toString();
say $tmDOM->findvalue('//checksum[checksumMethod="' . $hm .'"]/checksumValue');
say $prettyxml;
__DATA__
<checksum> <checksumMethod>SHA-1</checksumMethod> <checksumValue>56db195fc75e93509133193bfe5608eaeef2c471</checksumValue> </checksum>
The first say will correctly print out the hash value. The second say will print an empty string. Obviously, pretty_print changes all element values by padding them with whitespace to look pretty when printed. I could use "contains" in the xpath to solve this the dirty way, but that would be, well, dirty.
Now my first (and so far, only) idea was to simply copy the DOM object so that pretty_print can mess up one but spare the other, like this:
my $PRSR = XML::LibXML->new();
my $PP = XML::LibXML::PrettyPrint->new(indent_string => " ");
my $tmRawData = <DATA>;
my $tmDOM = $PRSR->load_xml(string => $tmRawData);
my $hm="SHA-1";
say $tmDOM->findvalue('//checksum[checksumMethod="' . $hm .'"]/checksumValue');
my $tmDOM2 = $tmDOM;
my $prettyxml = $PP->pretty_print($tmDOM2->documentElement())->toString();
say $tmDOM->findvalue('//checksum[checksumMethod="' . $hm .'"]/checksumValue');
__DATA__
<checksum> <checksumMethod>SHA-1</checksumMethod> <checksumValue>56db195fc75e93509133193bfe5608eaeef2c471</checksumValue> </checksum>
but this produces exactly the same result.
question: Why doesn't copying tmDOM to another variable tmDOM2 solve this?
Because you're copying a reference. It's like this:
my $x = [ 1 .. 9 ];
my $y = $x; # copy $x to $y to protect it from changes to $x
push @$x, 10; # change $x
print "$_\n" for @$y; # d'oh! we changed $y too
question: Is there a way to prevent pretty_print from messing up element values?
Read the documentation. In particular, search for the preserves_whitespace
option which allows you to tell XML::LibXML::PrettyPrint to preserve whitespace within particular elements.