Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: How to write an xpath query including attributes for an XML with namespace

by choroba (Canon)
on Sep 06, 2013 at 12:59 UTC ( #1052708=note: print w/ replies, xml ) Need Help??


in reply to How to write an xpath query including attributes for an XML with namespace

To use a namespace in an XPath expression, you have to register it first. The following works for me:

#!/usr/bin/perl use warnings; use strict; use XML::LibXML; my $file_dati = '/path/to/file.xml'; my $parser = 'XML::LibXML'->new(); my $doc = $parser->parse_file($file_dati) || die('dead1'); my $xc = 'XML::LibXML::XPathContext'->new($doc); $xc->registerNs('mw', 'http://myweb.com'); my $nodetobefound = '//mw:comment[@timestamp="20130903225513"]'; print "To be found: $nodetobefound\n"; my $commento = $xc->findnodes($nodetobefound) || die('dead3'); print $commento->get_node(0)->toString();

As Corion suggested, I changed the name of the element and added // for recursive search.

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


Comment on Re: How to write an xpath query including attributes for an XML with namespace
Download Code
Replies are listed 'Best First'.
Re^2: How to write an xpath query including attributes for an XML with namespace
by topola1882 (Initiate) on Sep 06, 2013 at 13:46 UTC

    Thanks Choroba, it worked!!

Re^2: How to write an xpath query including attributes for an XML with namespace
by Anonymous Monk on Sep 07, 2013 at 07:40 UTC

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2015-07-28 08:54 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 (254 votes), past polls