Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: XML::LibXML and namespaces

by choroba (Canon)
on Nov 09, 2012 at 09:51 UTC ( #1003092=note: print w/ replies, xml ) Need Help??


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.

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


Comment on Re: XML::LibXML and namespaces
Download Code
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!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1003092]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2015-07-07 02:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (86 votes), past polls