Subject: | Findnodes() searches entire document, even when run on a twig |
XML::Twig 3.23 doesn't seem to properly limit its findnodes() operation
to a given twig. Either that, or I've badly misunderstood findnodes().
Enclosed script reports that there are 4 spans in every section of a
document, while in fact there are 1, 2, and 1 respectively.
I rate this a critical defect because I can no longer trust the
operation of the key findnodes() routine.
Subject: | xttest.pl |
# Script that shows that either (i) XML::Twig 3.23 doesn't properly limit
# its findnodes() operation to a given twig, or that (ii) I have misread
# the findnodes() documentation and/or misunderstood its operation.
# This script reports that there are 4 spans in every div[@class='section']
# while in fact there are 1, 2, and 1 respectively. Somehow, findnodes()
# is searching the entire XML document, not just its respective twig.
use XML::Twig;
# use XML::Twig::XPath; # even if uncommented, does not seem to change the results
use strict;
my $xml = <<XML;
<container>
This is some text.
<div id="first" class='section'>Text and <span class="one">some spanned text</span>, OK?</div>
<div id="second" class='section'>Some text more text <span class="one">a span</span> and <span class="one">another span</span></div>
<div id="third" class='section'>Some text <span class="one">and a span</span></div>
Hey, more text!
</container>
XML
my $t = XML::Twig->new();
$t->parse($xml);
foreach (qw/first second third/) {
my @divs = $t->findnodes("//div[\@id='$_']");
my $d = shift @divs;
my @spans = $d->findnodes("//span");
print "findnodes() finds ", scalar(@spans), " spans in $_ DIV, yet the DIV's contents are:\n";
print "\t", $d->sprint, "\n\n";
}