http://www.perlmonks.org?node_id=1003092


in reply to XML::LibXML and namespaces

See the documentation of XML::LibXML::Node under findnodes. If you follow the advice given there, your XPath will work:
#!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $file = '1832.tcx'; my $parser = XML::LibXML->new->parse_file($file); my $xml = XML::LibXML::XPathContext->new; # No argum +ent here! $xml->registerNs('x', 'http://www.garmin.com/xmlschemas/TrainingCenterDatab +ase/v2'); for my $key ($xml->findnodes('//x:Lap', $parser)) { # Provide +the $parser here. my $string = $key->findvalue('@StartTime'); # No $pars +er needed, since attributes are namespaceless. print "1\t$string\n"; } my @X; for my $node ($xml->findnodes('//x:Trackpoint', $parser)) { # Again, $ +parser as argument. my $time = $xml->findvalue('x:Time', $node); # Context +specified as argument. push @X, $time; } print "@X\n";

Update: comments.

لսႽ† ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Replies are listed 'Best First'.
Re^2: XML::LibXML and namespaces
by Anonymous Monk on Nov 09, 2012 at 10:09 UTC

    Ah! That's what I was missing. I did try to put a context in, but I tried $xml, not $node.

    Thanks!