Possible bug in XML:LibXML

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::DOM 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.
 
D

Dr.Ruud

Fergus McMenemie schreef:
my $x=($root->getElementsByTagName("common-name"));

Maybe you meant:

my $x =()= $root->getElementsByTagName("common-name");

See also `perldoc -q count`.
 
X

xhoster

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.

Exactly where are you "seeing" this?
I have seen this behaviour repeated on
Mac 10.4 and Solaris 10. XML::DOM 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');

Where can we obtain camelids.xml in order to repeat your example?
(I've looked at the web site you linked to, and if it has it then
it isn't obvious.)
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!

Despite the parenthesis, the method is called in a scalar context. So
it should behave identically to the next piece of code.
my $y=$root->getElementsByTagName("common-name");
print "\ny=$y\n"; # should be a NodeList

Unless you know there is no stringification overload for NodeList, then
this print statement doesn't tell you much of anything about what $y
actually is. Try using Data::Dumper instead.

In any event, you have told us neither what you expected to see nor what
you actually did see, I have no idea how to go about explaining this
invisible and possibly nonexistent discrepancy.

Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 
F

Fergus McMenemie

Exactly where are you "seeing" this?
Here is what I see when I run the test script:-
../libxmltest.pl

44444444444444444444444444444444444444444444444444

x=Bactrian CamelDromedary, or Arabian CamelLlamaGuanacoVicuna

y=Bactrian CamelDromedary, or Arabian CamelLlamaGuanacoVicuna

z=XML::LibXML::Element=SCALAR(0x18427a4)
Bactrian Camel (Camelus bactrianus) endangered
Dromedary, or Arabian Camel (Camelus dromedarius) no special status
Llama (Lama glama) no special status
Guanaco (Lama guanicoe) special concern
Vicuna (Vicugna vicugna) endangered
Where can we obtain camelids.xml in order to repeat your example?
(I've looked at the web site you linked to, and if it has it then
it isn't obvious.)
Sorry about that. I think it is in the zip file. However it is simpler
to get it from
http://search.cpan.org/src/KHAMPTON/XML-SemanticDiff-0.95/eg/camelids.xm
l
Despite the parenthesis, the method is called in a scalar context. So
it should behave identically to the next piece of code.
Yep. Ok.
Unless you know there is no stringification overload for NodeList, then
this print statement doesn't tell you much of anything about what $y
actually is. Try using Data::Dumper instead. Tomorrow!

In any event, you have told us neither what you expected to see nor what
you actually did see, I have no idea how to go about explaining this
invisible and possibly nonexistent discrepancy.
I expected to see something similar to:-

44444444444444444444444444444444444444444444444444

x=XML::DOM::NodeList=ARRAY(0x18bb880)

y=XML::DOM::NodeList=ARRAY(0x180127c)

z=XML::DOM::Element=ARRAY(0x18fa3bc)
....... rest snipped....
 
X

xhoster

Sorry about that. I think it is in the zip file. However it is simpler
to get it from
http://search.cpan.org/src/KHAMPTON/XML-SemanticDiff-0.95/eg/camelids.xm
l

OK, thanks.
Tomorrow!

I'll save you the weekend. As I suspected, XML::LibXML::NodeList does have
stringification overloaded. So when interpolated into "", it doesn't use
the default reference/object stringification like
XML::DOM::NodeList=ARRAY(0x18bb880) does, but instead it just crams
together the string version of all its elements. I don't know the rational
for this decision, and I would probably disagree with it (I'd think I'd at
least use $" to join the strings, rather then ''). But it is what it is.

With Dumper:

bless( [
bless( do{\(my $o = 6429552)}, 'XML::LibXML::Element' ),
bless( do{\(my $o = 6430304)}, 'XML::LibXML::Element' ),
bless( do{\(my $o = 6430976)}, 'XML::LibXML::Element' ),
bless( do{\(my $o = 6430720)}, 'XML::LibXML::Element' ),
bless( do{\(my $o = 6430272)}, 'XML::LibXML::Element' )
], 'XML::LibXML::NodeList' );


Xho

--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top