title: Perl XML::LibXML $node->findnodes($xpath) finds nodes it shouldn't
body: <p>Here's some code I am having problems with, I process some XML and in a method in an OO class I extract an element from each of several nodes that repeat in the document. There should only be one such element in the subtree for each node but my code gets all elements as if it is operating on the document as a whole.</p>
  
  <p>Because I only expected to get oine element I only use the zeroth element of an array, this leads my function to output the wrong value (and its the same for all items in the document)</p>
  
  <p>Here's some simplified code that illustrates the problem</p>
  
  <pre><code>$ cat t4.pl
  #!/usr/bin/perl
  use strict;
  use warnings;
  use XML::LibXML;
  
  my $xml = &lt;&lt;EndXML;
  &lt;Envelope&gt;
    &lt;Body&gt;
      &lt;Reply&gt;
        &lt;List&gt;
          &lt;Item&gt;
            &lt;Id&gt;8b9a&lt;/Id&gt;
            &lt;Message&gt;
              &lt;Response&gt;
                &lt;Identifier&gt;55D&lt;/Identifier&gt;
              &lt;/Response&gt;
            &lt;/Message&gt;
          &lt;/Item&gt;
          &lt;Item&gt;
            &lt;Id&gt;5350&lt;/Id&gt;
            &lt;Message&gt;
              &lt;Response&gt;
                &lt;Identifier&gt;56D&lt;/Identifier&gt;
              &lt;/Response&gt;
            &lt;/Message&gt;
          &lt;/Item&gt;
        &lt;/List&gt;
      &lt;/Reply&gt;
    &lt;/Body&gt;
  &lt;/Envelope&gt;
  EndXML
  
  my $foo = Foo-&gt;new();
  
  my $parser = XML::LibXML-&gt;new();
  my $doc    = $parser-&gt;parse_string( $xml );
  my @list   = $doc-&gt;getElementsByTagName( 'Item' );
  
  for my $item ( @list ) {
  
      my $id = get( $item, 'Id' );
      my @messages = $item-&gt;getElementsByLocalName( 'Message' );
  
      for my $message ( @messages ) {
  
          my @children = $message-&gt;getChildNodes();
  
          for my $child ( @children ) {
  
              my $name = $child-&gt;nodeName;
  
              if ( $name eq 'Response' ) {
                  print "child is a Response\n";
                  $foo-&gt;do( $child, $id );
              }
              elsif ( $name eq 'text' ) {
  
                  # ignore whitespace between elements
              }
              else {
                  print "child name is '$name'\n";
              }
          }    # child
      }    # Message
  }    # Item
  
  # ..............................................
  
  sub get {
      my ( $node, $name ) = @_;
  
      my $value   = "(Element $name not found)";
      my @targets = $node-&gt;getElementsByTagName( $name );
  
      if ( @targets ) {
          my $target = $targets[0];
          $value = $target-&gt;textContent;
      }
  
      return $value;
  }
  
  # ..............................................
  
  package Foo;
  
  sub new {
      my $self = {};
      bless $self;
      return $self;
  }
  
  sub do {
      my $self = shift;
      my ( $node, $id ) = @_;
  
      print '-' x 70, "\n", ' ' x 12, $node-&gt;toString( 1 ), "\n", '-' x 70, "\n";
  
      my @identifiers = $node-&gt;findnodes( '//Identifier' );
      print "do() found ", scalar @identifiers, " Identifiers\n";
  
      print "$id, ", $identifiers[0]-&gt;textContent, "\n\n";
  }
  </code></pre>
  
  <p>Here's the output </p>
  
  <pre><code>$ perl t4.pl
  child is a Response
  ----------------------------------------------------------------------
              &lt;Response&gt;
                &lt;Identifier&gt;55D&lt;/Identifier&gt;
              &lt;/Response&gt;
  ----------------------------------------------------------------------
  do() found 2 Identifiers
  8b9a, 55D
  
  child is a Response
  ----------------------------------------------------------------------
              &lt;Response&gt;
                &lt;Identifier&gt;56D&lt;/Identifier&gt;
              &lt;/Response&gt;
  ----------------------------------------------------------------------
  do() found 2 Identifiers
  5350, 55D
  </code></pre>
  
  <p>I was expecting </p>
  
  <pre><code>do() found 1 Identifiers
  </code></pre>
  
  <p>I was expecting the last line to be</p>
  
  <pre><code>5350, 56D
  </code></pre>
  
  <p>I am using an old version of XML::LibXML due to platform issues.</p>
  
  <p><strong>Q: Does the problem exist in later versions or am I doing something wrong?</strong> </p>