2
votes

I'm trying to compare two nodes using 'isSameNode'. However, one of the nodes is created via 'parse_balanced_chunk'. From the http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-isSameNode document, is says "This method provides a way to determine whether two Node references returned by the implementation reference the same object."

So I'm wonder is it not working as I would expect because they are indeed coming from two different sources (one from the parsed txt file, the other from parse_balance_chunk)?

Here is the 'test_in.xml' & code:

<?xml version="1.0"?>
<TT>
 <A>ZAB</A>
 <B>ZBW</B>
 <C>
  <D>
   <E>ZSE</E>
   <F>ZLC</F>
  </D>
 </C>
 <C>
  <K>
   <H>dog</H>       
   <E>one123</E>       
   <M>bird</M>       
  </K>
 </C>
</TT>

use warnings;
use strict;
use XML::LibXML;

my $parser = XML::LibXML->new({keep_blanks=>(0)});
my $dom = $parser->load_xml(location => 'test_in.xml') or die;

#called in scalar context ($na1)
my ($na1) = $dom->findnodes('//D');
my ($na2) = $dom->findnodes('//D');
my $X1 = $na1->isSameNode($na2); ##MATCHES

my ($frg) = $parser->parse_balanced_chunk ("<D><E>ZSE</E><F>ZLC</F></D>");
my $X2 = $na1->isSameNode($frg); ##WHY NO MATCH?

my ($na3) = $frg->findnodes('//D');
my $X3 = $na1->isSameNode($na3); ##WHY NO MATCH?

print "SAME?: $X1\n";
print "SAME?: $X2\n";
print "SAME?: $X3\n";

And the output:

SAME?: 1
SAME?: 0
SAME?: 0

So the first 'isSameNode' test obviously MATCHES (same exact findnodes & xpath expression). But neither of the 2nd or 3rd 'isSameNode' tests work using the node from the 'parsed_balance_chunk'. Is it something simple I'm overlooking with the syntax or is it just that I can't compare two nodes this way? If not, what is the method for comparing two nodes? Waht I'm ultimately trying to determine if a block of xml code (i.e. from a previous parsed_balance_chuck) already exist in the xml file.

1

1 Answers

2
votes

Because they're not the same node. Like the name says, it checks if two nodes are the same node. You seem think think it checks if two nodes are equivalent, but that's not what it does.