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

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,

I have a bunch of XML files from my GPS, and I'd like to extract data from them, and play around with them, displaying them graphically for one. As it's done properly, it has its own schema, and uses its own namespace. One such file can be found at www.dehulst.nl/Garmin/TCX/1832.tcx

So, in order to parse such an XML file, you have to register it with XML::LibXML (using a variable $string as the prefix)

my $parser = XML::LibXML->new->parse_file($file); my $xml = XML::LibXML::XPathContext->new($parser); $xml->registerNs($string,'http://www.garmin.com/xmlschemas/TrainingCen +terDatabase/v2');
Now, extracting the value of an attribute poses no problem:
for my $key ($xml->findnodes('//x:Lap')) { $string = norm_date($key->findvalue("\@StartTime")); }
Just in case anyone is worried, there (usually) is only one Lap per file.

The problems start when I want to extract the timestamps from each recorded datapoint. My first try was

for my $node ($xml->findnodes('//y:Trackpoint')) { $time = $node->findvalue("Time"); push @X,$time; }
This fails. It does find the set of nodes, but fails to find the timestamp that's in the Time element. Experimenting with the examples I Googled, I found the following, which does give me the timestamps:
for my $node ($xml->findnodes("//y:Trackpoint")) { for my $ch ($node->childNodes) { $time = norm_date($ch->textContent)-$epoch if $ch->nodeName =~ /Time +/; } push @X,$time; }
That's nasty. It does work, but it's nasty.

So, the question is: why does the findvalue function fail to work with a non-default namespace? Or am I missing something?