F
Fergus McMenemie
The following test script is not doing what I expect. It must be
a bug The docs I have seen state that "In a SCALAR context
getElementsByTagName returns a XML::LibXML::NodeList object.
Instead I see it returning a string of the combined text objects
from all the child nodes. I have seen this behaviour repeated on
Mac 10.4 and Solaris 10. XML:OM behaves as documented.
use strict;
use XML::LibXML;
my($parser);
MAIN: {
$parser = XML::LibXML->new();
test4();
}
sub test4 {
print "\n","4"x50,"\n";
my $tree = $parser->parse_file('camelids.xml');
my $root = $tree->getDocumentElement;
my $x=($root->getElementsByTagName("common-name"));
print "\nx=$x\n"; # should be number of elements in an Element array..
# i think!
my $y=$root->getElementsByTagName("common-name");
print "\ny=$y\n"; # should be a NodeList
my($z)=$root->getElementsByTagName("common-name");
print "\nz=$z\n"; # should be 1st element of Element array
foreach my $camelid ($root->findnodes('species')) {
my $latin_name = $camelid->findvalue('@name');
my $common_name = $camelid->findvalue('common-name');
my $status = $camelid->findvalue('conservation/@status');
print "$common_name ($latin_name) $status \n";
}
}
The above example is based on a example from
http://www.xml.com/pub/a/2001/11/14/xml-libxml.html
I would be glad if any mistakes in what I have done were pointed
out. Or any hints on how I should take this further. I think I
see similar incorrect behaviour in find() and findnodes().
Thanks in advance Fergus.
a bug The docs I have seen state that "In a SCALAR context
getElementsByTagName returns a XML::LibXML::NodeList object.
Instead I see it returning a string of the combined text objects
from all the child nodes. I have seen this behaviour repeated on
Mac 10.4 and Solaris 10. XML:OM behaves as documented.
use strict;
use XML::LibXML;
my($parser);
MAIN: {
$parser = XML::LibXML->new();
test4();
}
sub test4 {
print "\n","4"x50,"\n";
my $tree = $parser->parse_file('camelids.xml');
my $root = $tree->getDocumentElement;
my $x=($root->getElementsByTagName("common-name"));
print "\nx=$x\n"; # should be number of elements in an Element array..
# i think!
my $y=$root->getElementsByTagName("common-name");
print "\ny=$y\n"; # should be a NodeList
my($z)=$root->getElementsByTagName("common-name");
print "\nz=$z\n"; # should be 1st element of Element array
foreach my $camelid ($root->findnodes('species')) {
my $latin_name = $camelid->findvalue('@name');
my $common_name = $camelid->findvalue('common-name');
my $status = $camelid->findvalue('conservation/@status');
print "$common_name ($latin_name) $status \n";
}
}
The above example is based on a example from
http://www.xml.com/pub/a/2001/11/14/xml-libxml.html
I would be glad if any mistakes in what I have done were pointed
out. Or any hints on how I should take this further. I think I
see similar incorrect behaviour in find() and findnodes().
Thanks in advance Fergus.