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)
Now, extracting the value of an attribute poses no problem: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');
Just in case anyone is worried, there (usually) is only one Lap per file.for my $key ($xml->findnodes('//x:Lap')) { $string = norm_date($key->findvalue("\@StartTime")); }
The problems start when I want to extract the timestamps from each recorded datapoint. My first try was
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')) { $time = $node->findvalue("Time"); push @X,$time; }
That's nasty. It does work, but it's nasty.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; }
So, the question is: why does the findvalue function fail to work with a non-default namespace? Or am I missing something?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: XML::LibXML and namespaces
by choroba (Cardinal) on Nov 09, 2012 at 09:51 UTC | |
by Anonymous Monk on Nov 09, 2012 at 10:09 UTC | |
Re: XML::LibXML and namespaces
by Anonymous Monk on Nov 09, 2012 at 11:30 UTC | |
Re: XML::LibXML and namespaces
by Lotus1 (Vicar) on Nov 09, 2012 at 13:29 UTC | |
by Anonymous Monk on Nov 11, 2012 at 13:33 UTC | |
by Anonymous Monk on Aug 19, 2014 at 13:20 UTC | |
by Anonymous Monk on Oct 03, 2014 at 12:30 UTC | |
by Anonymous Monk on Oct 04, 2014 at 06:19 UTC | |
by seanc (Novice) on Oct 04, 2014 at 08:45 UTC | |
by Anonymous Monk on Nov 09, 2012 at 13:47 UTC | |
by Lotus1 (Vicar) on Nov 09, 2012 at 13:50 UTC | |
by Anonymous Monk on Nov 09, 2012 at 14:32 UTC |
Back to
Seekers of Perl Wisdom